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
29
votes
2 answers

IndexOutOfRangeException when adding to Hashset

I have a simple application that adds about 7 million short strings to a HashSet. Occasionally I get an exception during a call to Hashset.Add(): System.Collections.Generic.HashSet`1.IncreaseCapacity(): Index was outside the bounds of the…
dcrobbins
  • 525
  • 4
  • 8
29
votes
1 answer

Set equivalent of WeakHashMap?

Is HashSet> the Set equivalent of WeakHashMap? That is, will entries be automatically deleted when they are no longer referenced? If not, what is the equivalent?
Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135
29
votes
8 answers

Find the Biggest number in HashSet/HashMap java

I would like to find the biggest number in HashSet and HashMap. Say I have the number [22,6763,32,42,33] in my HashSet and I want to find the largest number in my current HashSet..how would i do this? and Same thing for the HashMap as well. I hope…
user2064467
  • 375
  • 1
  • 4
  • 13
28
votes
7 answers

Why have HashSet but not Set in C#?

Old question My understanding is that C# has in some sense HashSet and set types. I understand what HashSet is. But why set is a separate word? Why not every set is HashSet? New question Why does C# has no generic Set type, similar to…
ilya n.
  • 18,398
  • 15
  • 71
  • 89
27
votes
2 answers

How to avoid unchecked cast warning when cloning a HashSet?

I'm trying to make a shallow copy of a HashSet of Points called myHash. As of now, I have the following: HashSet myNewHash = (HashSet) myHash.clone(); This code gives me an unchecked cast warning however. Is there a better way to do…
Tim
  • 4,295
  • 9
  • 37
  • 49
27
votes
1 answer

Unexpected running times for HashSet code

So originally, I had this code: import java.util.*; public class sandbox { public static void main(String[] args) { HashSet hashSet = new HashSet<>(); for (int i = 0; i < 100_000; i++) { hashSet.add(i); …
scdivad
  • 421
  • 3
  • 10
27
votes
2 answers

Union vs Unionwith in HashSet

What the difference between HashSet.Union vs HashSet.Unionwith when i combine 2 hashsets. I am trying to combine like this: HashSet enginesSupportAll = _filePolicyEvaluation.EnginesSupportAll; enginesSupportAll =…
Max.Futerman
  • 1,012
  • 2
  • 9
  • 30
25
votes
2 answers

How can a pathological input exist for an std::unordered_set?

I was solving the basic problem of finding the number of distinct integers in a given array. My idea was to declare an std::unordered_set, insert all given integers into the set, then output the size of the set. Here's my code implementing this…
25
votes
3 answers

Why is Python set intersection faster than Rust HashSet intersection?

Here is my Python code: len_sums = 0 for i in xrange(100000): set_1 = set(xrange(1000)) set_2 = set(xrange(500, 1500)) intersection_len = len(set_1.intersection(set_2)) len_sums += intersection_len print len_sums Here is my Rust…
Akavall
  • 82,592
  • 51
  • 207
  • 251
25
votes
10 answers

Convert comma separated string into a HashSet

So, how would you go about converting String csv = "11,00,33,66,44,33,22,00,11"; to a hashset in the quickest-most optimized way. This is for a list of user-ids. Update I ran all the answers provided through a test program where each method was…
Menelaos
  • 23,508
  • 18
  • 90
  • 155
24
votes
6 answers

Mutable objects and hashCode

Have the following class: public class Member { private int x; private long y; private double d; public Member(int x, long y, double d) { this.x = x; this.y = y; this.d = d; } @Override public int hashCode() { final int prime =…
robert
  • 243
  • 1
  • 2
  • 5
24
votes
9 answers

Iteration order of HashSet

If every object added to a java.util.HashSet implements Object.equals() and Object.hashCode() in a deterministic fashion, is the iteration order over the HashSet guaranteed to be identical for every identical set of elements added, irrespective of…
eljenso
  • 16,789
  • 6
  • 57
  • 63
23
votes
4 answers

Moving data from a HashSet to ArrayList in Java

I have the following Set in Java: Set< Set > SetTemp = new HashSet< Set >(); and I want to move its data to an ArrayList: ArrayList< ArrayList< String > > List = new ArrayList< ArrayList< String > >); Is it possible to do that ?
Ghadeer
  • 608
  • 2
  • 7
  • 21
22
votes
2 answers

Removing null references from a HashSet

Is there a simple way of removing null references from a HashSet like the way we can delete them from a List using list.removeAll(Collections.singletonList(null)) ?
Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
22
votes
1 answer

The difference between 'HashSet' and 'Set' in Scala?

I'm very confused by Scala's HashSet and Set types as they both seem to do the same thing. What is the difference between them? Is it the same in Java? In my reference it says that HashSet is an "explicit set class" (as compared to Set). What does…
Kristina
  • 15,859
  • 29
  • 111
  • 181