Most programming languages have types like HashMap
(also known as Dictionary
, Hashtable
or map
) and HashSet
(also known as set
). The map
types can store both a key and a value, and the set
types just store a key. No duplicate keys are allowed—these collections check for uniqueness.
It seems like it is common sense to do certain things with a map
, and other things with a set
. But is this really necessary? It is possible to do everything with just the map
types, and never use the set
types. We can just use any value—like a bool
with the value true
, or the integer 1.
In any case, we typically use sets when:
Intersect
or Overlaps
are required.In my experience, normal programs rarely require mathematical set operations. And it makes about as much sense just to use true
to indicate a value instead of using an entirely different type like HashSet
. There is no likely performance impact. If we are using a Dictionary
elsewhere, it may reduce code size to reuse that class for set operations, however.