Table of Contents
One of the common interview question is “What is difference between HashMap and HashSet”..Before we actually see differences,let me give you brief introduction of both.
Java HashMap:
- HashMap in java
- How HashMap works in java
- hash and indexfor method in HashMap
- hashcode and equals method in java
- How to sort HashMap by keys and values
- Difference between HashMap and HashSet
- Difference between HashMap and Hashtable
- How to iterate over HashMap
HashMap
HashMap implements Map interface which maps key to value.It is not synchronized and is not thread safe.Duplicate keys are not allowed and null keys as well as values are allowed. For more details, you can also read How HashMap works in java.
1 2 3 4 5 |
HashMap<Interger,String> employeeHashmap=new HashMap<Integer,String>(); employeeHashmap.put(1,"Arpit"); employeeHashmap.put(2,"John"); |
HashSet
HashSet implements Set interface which does not allow duplicate value.It is not synchronized and is not thread safe.For more details, you can also read How HashSet works in java.
1 2 3 4 5 6 |
HashSet employeeSet=new HashSet(); employeeSet.add("Arpit"); employeeSet.add("Arpit"); employeeSet.add("john"); |
Above employeeSet will have 2 elements in it as Set does not allow duplicate values.
add method is used to add element to HashSet.If It return true then element is added successfully but if it return false then you are trying to insert duplicate value.
1 2 3 |
public boolean add(Object o) |
One main thing about HashSet is objects which we are going to add in HashSet must implement Hashcode() and equals() method so that we can check for duplicate values.If we are adding custom objects to HashSet then we must override() Hashcode() and equals() method according to our need.If we do not override then object will take default implementation which may not desirable.
HashMap vs HashSet:
Parameter
|
HashMap
|
HashSet
|
---|---|---|
Interface
|
This is core difference among them.HashMap implements Map interface
|
HashSet implement Set interface
|
Method for storing data
|
It stores data in a form of key->value pair.So it uses put(key,value) method for storing data
|
It uses add(value) method for storing data
|
HashMap allows duplicate value but not duplicate keys
|
HashSet does not allow duplicate values.
|
|
Performance
|
It is faster than hashset as values are stored with unique keys
|
It is slower than HashMap
|
HashCode Calculation
|
In hash map hashcode value is calculated using key object
|
In this,hashcode is calculated on the basis of value object.Hashcode can be same for two value object so we have to implement equals() method.If equals() method return false then two objects are different.
|
Please go through  core java interview questions for more interview questions.
HashCode Calculation
In hash map hashcode value is calculated using key object
In this,hashcode is calculated on the basis of value object.Hashcode can be same for two value object so we have to implement equals() method.If equals() method return false then two objects are different.
This is not the correct difference which is written here. Please correct it.
And this is very useful tutorial no doubt. each topic described in detail. Thanks for such a nice tutorial.