Difference Between HashMap and HashSet in Java

Connect with

Difference between HashMap and HashSet in JavaDifference between HashMap and HashSet in Java is one of the most frequently asked interview question during any level of core java interview. Usually, Collection framework and multi-threading interview questions are mostly on demand for core java interview question.

1. HashMap and HashSet overview

Both HashMap and HashSet in Java are integral part of Collection framework in java. These are most frequently used collection classes, it means , HashMap from Map , HashSet from Set and ArrayList from List in java.

Let us go through one by one both of these i.e. HashMap and HashSet and afterward will go for Differences between them i.e. HashSet and HashMap

2. HashMap in Java

HashMap is an implementation of Map Interface, store key and value , it means, maps a key to its value. Basically, map Interface has three implementation classes HashMap, LinkedHashMap and TreeMap. It works on hasing technique, so complexty of getting element is O(1).

Key Points of HashMap are:

  • Duplicate keys are not allowed in a map i.e. HashMap but duplicate values are allowed.
  • HashMap not maintain insertion order.
  • HashMap allows null values and one null keys.
  • HashMap is not synchronized, but collection framework provide way to make them synchronized using Collections.synchronizedMap().
  • public Object put(Object Key,Object value) method is used to add an element in the map.

3. HashSet in Java

HashSet is implementation of Set Interface which does not allow duplicate value. basic fundamental is Set does not allow null, duplicate element and not maintain insertion order , maintain sorting order.

Key Points about HashSet in Java are:

  • Does not allow null
  • it maintain sorting order not insertion order
  • objects which are going to be stored in HashSet must override equals() and hashCode() method, so that , can check for equality and no duplicate value will stored in set.
  • add(Object o) returns true if unique value added , if duplicate then returns false.

4. Difference between HashMap and HashSet in Java

Following are the key differences between HashMap vs HashSet in Java

  • HashMap is an implementation of Map interface while HashSet is an implementation of Set Interface
  • HashMap Stores data in form of key-value pair while HashSet Store only objects
  • HashMap is faster than HashSet because unique key is used to access object while HashSet is slower than Hashmap
  • Put method in HashMap in Java is used to add element in map while add method in HashSet is used to add element is Set
  • In hashMap hashcode value is calculated using key object while in HashSet member object is used for calculating hashcode value which can be same for two objects so equal() method is used to check for equality if it returns false that means two objects are different.

5. Differences between some java classes

You can visit other differences between two java classes:

Please let me know by writing comment on this post, if you want to add any other difference between HashSet and HashMap in Java or any suggestions.


Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *