The Hashset
Internally implements HashMap
. If you see the internal implementation the values inserted in HashSet are stored as keys in the HashMap and the value is a Dummy object of Object class.
Difference between HashMap vs HashSet is:-
HashMap
contains key value pairs and each value can be accessed by key where as HashSet needs to be iterated everytime as there is no get method.HashMap
implements Map interface and allows one null value as a key and multiple null values as values.Where as HashSet
implements Set interface, allows only one null value and no duplicated values.(Remeber one null key is allowed in HashMap key hence one null value in HashSet as HashSet implemements HashMap internally). HashSet
and HashMap
does not maintain the order of insertion while iterating.