Difference between HashSet and HashMap?

SpikETidE picture SpikETidE · May 5, 2010 · Viewed 245.6k times · Source

Apart from the fact that HashSet does not allow duplicate values, what is the difference between HashMap and HashSet?

I mean implementation wise? It's a little bit vague because both use hash tables to store values.

Answer

b.roth picture b.roth · May 5, 2010

HashSet is a set, e.g. {1,2,3,4,5}

HashMap is a key -> value (key to value) map, e.g. {a -> 1, b -> 2, c -> 2, d -> 1}

Notice in my example above that in the HashMap there must not be duplicate keys, but it may have duplicate values.

In the HashSet, there must be no duplicate elements.