What are the Differences Between a HashSet and a HashMap in Java ?

The main difference between a HashSet and a HashMap are...

Base class : A HashSet class implements the Set interface. Whereas a HashMap class implements the Map interface.

Storage : A HashSet is used to store distinct objects. A HashMap is used for storing key & value pairs, so that these can be retrieved by key later on.

Duplicate Elements : A HashSet does not allow storing duplicate elements. A HashMap also does not allow duplicate keys. But we can store duplicate values in a HashMap.

Null Elements : In a HashSet we can store a single null value. In a HashMap we can store single null key, but any number of null values.

Element Type : A HashSet contains only values of objects as its elements. Whereas a HashMap contains entrie (key value pairs).

Iteration : By using an Iterator we can iterate a HashSet. But a HashMap has to be converted into Set for iteration.



You May Interest

How Can We Take Thread Dump in Java ?

When is UnsupportedOperationException Thrown in Java ?

What is a CAS Operation in Java ?

What is a Directive in JSP ?

What is the Difference Between an Iterator and ListIterator in Ja ...