A HashSet
has an internal structure (hash), where items can be searched and identified quickly. The downside is that iterating through a HashSet
(or getting an item by index) is rather slow.
So why would someone want be able to know if an entry already exists in a set?
One situation where a HashSet
is useful is in getting distinct values from a list where duplicates may exist. Once an item is added to the HashSet
it is quick to determine if the item exists (Contains
operator).
Other advantages of the HashSet
are the Set operations: IntersectWith
, IsSubsetOf
, IsSupersetOf
, Overlaps
, SymmetricExceptWith
, UnionWith
.
If you are familiar with the object constraint language then you will identify these set operations. You will also see that it is one step closer to an implementation of executable UML.