Difference between HashSet and HashMap in Java
GIAN Tutorials
4:00 AM
Similarities on HashMap and HashSet in Java 1) Both HashMap and HashSet are a hash based collection in Java. 2) Both HashMap and HashSet are not synchronized and can not be shared between multiple threads. 3) Iterator returned by HashMap's keySet() and HashSet are fail-fast and they throw ConcurrentModificationException if they detect any structural change in Collection. 4) Both HashMap and HashSet provided constant time performance for basic operations like put(), get() etc. 5) Both HashSet and HashMap allows null values. Differences between HashSet and HashMap in Java 1) The first and most significant difference between HashMap and HashSet is that HashMap is an implementation of Map interface while HashSet is an implementation of Set interface, which means HashMap is a key value based data-structure and HashSet guarantees uniqueness by not allowing duplicates.In reality, HashSet is a wrapper around HashMap in Java, if you look at the code of add(E e) method