I know HashSet
is the implementation of the Set
interface, but I have a problem understanding the "hash". It seems to work the same as a Set
, but why it is called "hash"?
Asked
Active
Viewed 163 times
1 Answers
1
There are many ways to implement a Set interface. HashSet
is one of them. It uses an underlying HashMap
to store the elements of the Set. The "hash" part refers to the hashCode
used to map each element to an index of an array, to allow efficient lookup.

Eran
- 387,369
- 54
- 702
- 768
-
so in the underlying hashmap the key is the value and the value part is the index of the array or key is the index of the array and the value part is the value? – sevenxuguang Dec 20 '15 at 15:59
-
@sevenxuguang The element of the HashSet is the key in the underlying HashMap. The value is just a dummy object. The index of the array is calculated as a function of the element's `hashCode`. – Eran Dec 20 '15 at 16:02