Questions tagged [hashset]

A HashSet encapsulates operations that allow for the comparison of elements in collections. HashSets are frequently used to determine overlapping and unique elements within a collection.

HashSet<T> was introduced in .NET Framework 3.5 as part of the System.Collections.Generic namespace. HashSet is an unordered collection containing unique elements and provides a set of standard set operators such as intersection and union (plus many more). It has the standard collection operations Add (although this method returns a Boolean indicating whether or not that element already existed in the collection), Remove, and Contains, but because it uses a hash-based implementation for object identity, these operations are immediately accessible without looping the entire list as occurs with the List<T> collection for example (O(1) rather than O(n)).

Source: Linq to Objects using C# 4.0, Troy Magennis, Addison Wesley, 2010, Pearson Education Inc, ISBN-13: 978-0-321-63700-0

References

2277 questions
43
votes
3 answers

HashSet does not seem to realize that two objects are the same.

I'm trying to use HashSet to store objects of a class that I created, but apparently the same objects seem to have two different hashes, which is why the contains method does not realize that the object is already in the HashSet. This leads to my…
efficiencyIsBliss
  • 3,043
  • 7
  • 38
  • 44
42
votes
6 answers

Efficient way to clone a HashSet?

A few days ago, I answered an interesting question on SO about HashSet. A possible solution involved cloning the hashset, and in my answer I suggested to do something like this: HashSet original = ... HashSet clone = new…
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
42
votes
4 answers

Hash Set and Array List performances

I have implemented a method which simply loops around a set of CSV files that contain data on a number of different module. This then adds the 'moduleName' into a hashSet. (Code shown below) I have used a hashSet as it guarantees no duplicates are…
user1339335
  • 451
  • 1
  • 4
  • 3
41
votes
3 answers

Converting from HashSet to String[]

What's the best way to convert HashSet to String[]?
Jack B.
  • 937
  • 2
  • 8
  • 10
38
votes
4 answers

How to implement ConcurrentHashSet in .Net

I am trying to implement a ConcurrentHashSet in the spirit of ConcurrentDictionary, approach taken is to use a internal backing ConcurrentDictionary and write small delegating methods, this is how far i got, but well the set theoretic methods are I…
Sebastian
  • 6,293
  • 6
  • 34
  • 47
36
votes
12 answers

Why can't I retrieve an item from a HashSet without enumeration?

I'm looking for insight into the heads of HashSet designers. As far as I am aware, my question applies to both Java and C# HashSets, making me think there must be some good reason for it, though I can't think of any myself. After I have inserted an…
sooniln
  • 14,607
  • 4
  • 29
  • 35
34
votes
6 answers

How does HashSet not allow duplicates?

I was going through the add method of HashSet. It is mentioned that If this set already contains the element, the call leaves the set unchanged and returns false. But the add method is internally saving the values in HashMap public boolean add(E…
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
34
votes
6 answers

HashSet Iterating While Removing Items in C#

I have a hashset in C# that I'm removing from if a condition is met while iterating though the hashset and cannot do this using a foreach loop as below. foreach (String hashVal in hashset) { if (hashVal == "somestring") { …
aHunter
  • 3,490
  • 11
  • 39
  • 46
34
votes
11 answers

These Sets allow null. Why can't I add null elements?

I want to know why HashSet, LinkedHashSet and TreeSet implementation does not allow null elements? Whenever i try to run the following code it throws a null pointer exception. public static void main(String[] args) { HashSet hashSet =…
nakul
  • 1,445
  • 7
  • 20
  • 30
33
votes
7 answers

is Java HashSet thread-safe for read only?

If I have an instance of an HashSet after I ran it through Collections.unmodifiableSet(), is it thread-safe? I'm asking this since Set documentation states that it's not, but I'm only performing read operations.
Asaf Mesika
  • 1,643
  • 4
  • 20
  • 33
32
votes
6 answers

.NET: How to efficiently check for uniqueness in a List of 50,000 items?

In some library code, I have a List that can contain 50,000 items or more. Callers of the library can invoke methods that result in strings being added to the list. How do I efficiently check for uniqueness of the strings being added? Currently,…
Cheeso
  • 189,189
  • 101
  • 473
  • 713
31
votes
6 answers

Is there a way to get the difference between two sets of objects in c#

I want to get the difference between two sets of ints in c#. Given s1 and s2 I want to return those ints which are in s1 and not in s2. I can do something such as: List s1 = new List(); List s2 = new List(); foreach…
SiC
  • 457
  • 1
  • 9
  • 14
31
votes
3 answers

How can I insert all values of one HashSet into another HashSet?

I have two HashSets and I would like to implement a = a U b. If possible, I'd like to use HashSet::union rather than loops or other tweaks. I tried the following: use std::collections::HashSet; let mut a: HashSet = [1, 2,…
m.raynal
  • 2,983
  • 2
  • 21
  • 34
31
votes
5 answers

What is the difference between set and hashset in C++ STL?

When should I choose one over the other? Are there any pointers that you would recommend for using the right STL containers?
kal
  • 28,545
  • 49
  • 129
  • 149
30
votes
5 answers

What is the lookup time complexity of HashSet(IEqualityComparer)?

In C#.NET, I like using HashSets because of their supposed O(1) time complexity for lookups. If I have a large set of data that is going to be queried, I often prefer using a HashSet to a List, since it has this time complexity. What confuses me is…
Kirby
  • 3,649
  • 3
  • 26
  • 28