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

How do I convert a HashSet of Strings into a Vector?

I'm trying to convert a HashSet into a sorted vector that can then be joined with commas: use std::collections::HashSet; fn main() { let mut hs = HashSet::::new(); hs.insert(String::from("fee")); …
Ralph
  • 31,584
  • 38
  • 145
  • 282
22
votes
4 answers

How to Initialize Values to a HashSet in C#

I am using VS 2008 and I need to know how to initialize the HashSet. I know Some values which is needed to add it during initialization. How can I add values to the tblNames. System.Collections.Generic.HashSet tblNames; …
kbvishnu
  • 14,760
  • 19
  • 71
  • 101
22
votes
2 answers

Why is HashMap faster than HashSet?

I have been reading/researching the reason why HashMapis faster than HashSet. I am not quite understanding the following statements: HashMap is faster than HashSet because the values are associated to a unique key. In HashSet, member object is used…
runcode
  • 3,533
  • 9
  • 35
  • 52
21
votes
7 answers

How to convert hash Set into array using toArray() if the method toArray is not specified?

Looking at the java api for java collections framework, I could not find toArray() method in HashSet, there is toArray() method in abstract class Set. class Ideone { public static void main (String[] args) throws java.lang.Exception { …
ERJAN
  • 23,696
  • 23
  • 72
  • 146
21
votes
5 answers

Why initialize HashSet<>(0) to zero?

I love a HashSet<>() and use this eagerly while initializing this with the default constructor: Set users = new HashSet<>(); Now, my automatic bean creator (JBoss tools) initializes this as: Set users = new HashSet<>(0); Why the…
Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
20
votes
4 answers

How do HashSets in Java work?

Possible Duplicate: How does Java hashmap work? Can someone explain to me how HashSets in java work and why they are faster than using ArrayLists?
madCode
  • 3,733
  • 5
  • 26
  • 31
20
votes
6 answers

Subtract HashSets (and return a copy)?

I've got a HashSet, var universe = new HashSet(); And a bunch of subsets, var sets = new List>(numSets); I want to subtract a chunk, which I can do like this: var remaining = universe.ExceptWith(sets[0]); But ExceptWith works…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
20
votes
4 answers

Select an element by index from a .NET HashSet

At the moment I am using a custom class derived from HashSet. There's a point in the code when I select items under certain condition: var c = clusters.Where(x => x.Label != null && x.Label.Equals(someLabel)); It works fine and I get those…
Ventus
  • 2,482
  • 4
  • 35
  • 41
20
votes
3 answers

ordering a hashset example?

I need an example on how to use a comparable class on a HashSet to get an ascending order. Let’s say I have a HashSet like this one: HashSet hs = new HashSet(); How can I get hs to be in ascending order?
Jony
  • 6,694
  • 20
  • 61
  • 71
20
votes
3 answers

Element is present but `Set.contains(element)` returns false

How can an element not be contained in the original set but in its unmodified copy? The original set does not contain the element while its copy does. See image. The following method returns true, although it should always return false. The…
wehnsdaefflae
  • 826
  • 2
  • 12
  • 27
20
votes
3 answers

Java: Duplicate objects getting added to set?

If I run the below code then the output is 2 which means that the set contains 2 elements. However I think that set should contain 1 since both the objects are equal based on hashcode() value as well as .equals() method. Seems like some obvious…
snow_leopard
  • 1,466
  • 2
  • 20
  • 36
19
votes
4 answers

std::hash_set vs std::unordered_set, are they the same thing?

I know hash_set is non-standard and unordered_set is standard. However, I am wondering, performance wise, what is the difference between the two? Why do they exist separately?
unixman83
  • 9,421
  • 10
  • 68
  • 102
19
votes
3 answers

HashSet load factor

If I use a HashSet with a initial capacity of 10 and a load factor of 0.5 then every 5 elements added the HashSet will be increased or first the HashSet is increased of 10 elements and after at 15 at 20 atc. the capacity will be increased?
xdevel2000
  • 20,780
  • 41
  • 129
  • 196
19
votes
9 answers

Internal implementation of java.util.HashMap and HashSet

I have been trying to understand the internal implementation of java.util.HashMap and java.util.HashSet. Following are the doubts popping in my mind for a while: Whats is the importance of the @Override public int hashcode() in a HashMap/HashSet?…
peakit
  • 28,597
  • 27
  • 63
  • 80
19
votes
1 answer

Internal System.Linq.Set vs public System.Collections.Generic.HashSet

Check out this piece of code from Linq.Enumerable class: static IEnumerable DistinctIterator(IEnumerable source, IEqualityComparer comparer) { Set set = new Set(comparer); foreach…
HuBeZa
  • 4,715
  • 3
  • 36
  • 58