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
59
votes
6 answers

Would a Java HashSet's contains() method test equality of the strings or object identity?

Let's say I have this code in Java: HashSet wordSet = new HashSet(); String a = "hello"; String b = "hello"; wordSet.add(a); Would wordSet.contains(b); return true or false? From what I understand, a and b refer to different objects…
OkonX
  • 849
  • 1
  • 7
  • 10
59
votes
2 answers

Contains of HashSet in Python

In Java we have HashSet, I need similar structure in Python to use contains like below: A = [1, 2, 3] S = set() S.add(2) for x in A: if S.contains(x): print "Example" Could you please help?
Borys Stepov
  • 711
  • 1
  • 5
  • 4
55
votes
4 answers

Should a HashSet be allowed to be added to itself in Java?

According to the contract for a Set in Java, "it is not permissible for a set to contain itself as an element" (source). However, this is possible in the case of a HashSet of Objects, as demonstrated here: Set mySet = new…
davidmerrick
  • 1,027
  • 1
  • 10
  • 17
54
votes
5 answers

Why can't I preallocate a hashset

Why can't I preallocate a hashset? There are times when i might be adding a lot of elements to it and i want to eliminate resizing.
EpiX
  • 1,281
  • 2
  • 16
  • 22
54
votes
6 answers

HashSet vs ArrayList contains performance

When processing large amounts of data I often find myself doing the following: HashSet set = new HashSet (); //Adding elements to the set ArrayList list = new ArrayList (set); Something like "dumping" the contents of…
Jorge
  • 1,574
  • 2
  • 12
  • 14
54
votes
3 answers

How to convert List to HashSet in C#?

I have a List that has duplicates of objects. To solve that, I need to convert the List into a HashSet (in C#). Does anyone know how?
mathenthusiast8203
  • 755
  • 1
  • 7
  • 14
52
votes
3 answers

Java HashSet with a custom equality criteria?

I was looking for something akin to the Java TreeSet's ability to receive a custom comparator at instantiation time, so I needed not to use the object's default equality (and hash code) criteria. The closest I could come up with was to wrap my…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
50
votes
4 answers

Convert a HashSet to an array in .NET

How do I convert a HashSet to an array in .NET?
Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
47
votes
5 answers

Hashcode and Equals for Hashset

Please clarify my doubt in Hashset. Consider the following code, class Person { String name; Person(String n) { name=n; } public String getName() { return name; } @Override public boolean…
Lolly
  • 34,250
  • 42
  • 115
  • 150
47
votes
7 answers

Why does HashSet implementation in Sun Java use HashMap as its backing?

Looking at the source of Java 6, HashSet is actually implemented using HashMap, using dummy object instance on every entry of the Set. I think that wastes 4 byte (on 32-bit machines) for the size of the entry itself. But, why is it…
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228
46
votes
7 answers

HashSet allows duplicate item insertion - C#

This kind of seems like a noob question, but I could not find an answer for this question specifically. I have this class: public class Quotes{ public string symbol; public string extension } And am using this: HashSet values =…
jpints14
  • 1,361
  • 6
  • 15
  • 24
46
votes
8 answers

How can I convert a Java HashSet to a primitive int array?

I've got a HashSet with a bunch of Integers in it. I want to turn it into an array, but calling hashset.toArray(); returns an Object[]. Is there a better way to cast it to an array of int other than iterating through every element…
jackbot
  • 2,931
  • 3
  • 27
  • 35
44
votes
6 answers

What's better for creating distinct data structures: HashSet or Linq's Distinct()?

I'm wondering whether I can get a consensus on which method is the better approach to creating a distinct set of elements: a C# HashSet or using IEnumerable's .Distinct(), which is a Linq function? Let's say I'm looping through query results from…
Matt
  • 23,363
  • 39
  • 111
  • 152
44
votes
4 answers

Hashtable, HashMap, HashSet , hash table concept in Java collection framework

I am learning Java Collection Framework and got moderated understanding. Now, when I am going a bit further I got some doubts in: HashMap, HashSet, Hashtable. The Javadoc for HashMap says: Hash table based implementation of the Map interface. This …
CuriousMind
  • 8,301
  • 22
  • 65
  • 134
44
votes
3 answers

Java all determine elements are same in a list

I am trying to determine to see if all elements in a list are same. such as: (10,10,10,10,10) --> true (10,10,20,30,30) --> false I know hashset might be helpful, but i don't know how to write in java. this is the one I've tried, but didn't…
Colin Zhong
  • 727
  • 1
  • 7
  • 16