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
905
votes
24 answers

How to initialize HashSet values by construction?

I need to create a Set with initial values. Set h = new HashSet(); h.add("a"); h.add("b"); Is there a way to do this in one line of code? For instance, it's useful for a final static field.
Serg
  • 13,470
  • 8
  • 36
  • 47
684
votes
10 answers

Why there is no ConcurrentHashSet against ConcurrentHashMap

HashSet is based on HashMap. If we look at HashSet implementation, everything is been managed under HashMap. is used as a key of HashMap. And we know that HashMap is not thread safe. That is why we have ConcurrentHashMap in…
Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
518
votes
14 answers

Hashset vs Treeset

I've always loved trees, that nice O(n*log(n)) and the tidiness of them. However, every software engineer I've ever known has asked me pointedly why I would use a TreeSet. From a CS background, I don't think it matters all that much which you use,…
heymatthew
  • 6,716
  • 5
  • 26
  • 22
453
votes
3 answers

Is there an AddRange equivalent for a HashSet in C#

With a list you can do: list.AddRange(otherCollection); There is no add range method in a HashSet. What is the best way to add another ICollection to a HashSet?
Stefanos Kargas
  • 10,547
  • 22
  • 76
  • 101
440
votes
5 answers

Define: What is a HashSet?

HashSet The C# HashSet data structure was introduced in the .NET Framework 3.5. A full list of the implemented members can be found at the HashSet MSDN page. Where is it used? Why would you want to use it?
001
  • 62,807
  • 94
  • 230
  • 350
323
votes
8 answers

How to Iterate over a Set/HashSet without an Iterator?

How can I iterate over a Set/HashSet without the following? Iterator iter = set.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); }
user1621988
  • 4,215
  • 8
  • 27
  • 31
288
votes
2 answers

How to calculate the intersection of two sets?

Possible Duplicate: Efficiently finding the intersection of a variable number of sets of strings Say, have two Hashset, how to calculate the intersection of them? Set s1 = new HashSet(); Set s2 = new…
user496949
  • 83,087
  • 147
  • 309
  • 426
213
votes
20 answers

Difference between HashSet and HashMap?

Apart from the fact that HashSet does not allow duplicate values, what is the difference between HashMap and HashSet? I mean implementation wise? It's a little bit vague because both use hash tables to store values.
SpikETidE
  • 6,711
  • 15
  • 46
  • 62
194
votes
8 answers

What is the difference between HashSet and List?

Can you explain what is the difference between HashSet and List in .NET? Maybe you can explain with an example in what cases HashSet should be preferred against List ?
pencilCake
  • 51,323
  • 85
  • 226
  • 363
174
votes
10 answers

HashSet vs LinkedHashSet

What is the difference between them? I know that A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. Use this class instead of HashSet when you care about the iteration order. When you…
Shikarn-O
  • 3,337
  • 7
  • 26
  • 27
172
votes
2 answers

Why is HashSet so much slower than HashSet?

I wanted to store some pixels locations without allowing duplicates, so the first thing comes to mind is HashSet or similar classes. However this seems to be very slow compared to something like HashSet. For example, this…
160
votes
5 answers

How does HashSet compare elements for equality?

I have a class that is IComparable: public class a : IComparable { public int Id { get; set; } public string Name { get; set; } public a(int id) { this.Id = id; } public int CompareTo(object obj) { …
nima
  • 6,566
  • 4
  • 45
  • 57
158
votes
9 answers

Does adding a duplicate value to a HashSet/HashMap replace the previous value

Please consider the below piece of code: HashSet hs = new HashSet(); hs.add("hi"); -- (1) hs.add("hi"); -- (2) hs.size() will give 1 as HashSet doesn't allow duplicates so only one element will be stored. I want to know if we add the duplicate…
Anand
  • 20,708
  • 48
  • 131
  • 198
148
votes
11 answers

When should I use the HashSet type?

I am exploring the HashSet type, but I don't understand where it stands in collections. Can one use it to replace a List? I imagine the performance of a HashSet to be better, but I couldn't see individual access to its elements. Is it only…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
146
votes
21 answers

How to sort a HashSet?

For lists, we use the Collections.sort(List) method. What if we want to sort a HashSet?
Diana
  • 1,555
  • 4
  • 11
  • 17
1
2 3
99 100