Difference between HashMap and IdentityHashMap in Java
HashMap in java is a class that is a part of the java collection. It implements the Map interface of java. It stores the data in the form of key and value pair. The key should be unique but the value may be duplicate. If you try to insert the duplicate key, it will replace the element of the corresponding key. HashMap is similar to the hash table, but it is unsynchronized. It allows to store the null keys as well null values, but there should be only one null key and there can be any number of null values.
IdentityHashMap implements the Map interface. It follows reference-equality in place of object-equality when comparing keys (and values). This class is used when the user requires the objects to be compared via reference. It is not synchronized and must be synchronized externally. The iterators in this class are fail-fast, throw ConcurrentModificationException in an attempt to modify while iterating.
HashMap and IdentityHashMap both are the classes that implement the Map interface. But there are few differences that exist between them.
Example 1: HashMap
Java
import java.util.HashMap;
public class GFG {
public static void main(String[] args)
{
HashMap<Integer, String> map = new HashMap<>();
map.put( 10 , "Geeks" );
map.put( 20 , "for" );
map.put( 30 , "geeks" );
map.put( 40 , "welcome" );
map.put( 50 , "you" );
System.out.println( "Size of map is:- "
+ map.size());
System.out.println( "HashMap content: " + map);
map.remove( 50 );
System.out.println( "HashMap after removal : "
+ map);
}
}
|
Output
Size of map is:- 5
HashMap content: {50=you, 20=for, 40=welcome, 10=Geeks, 30=geeks}
HashMap after removal : {20=for, 40=welcome, 10=Geeks, 30=geeks}
Example 2: IdentityHashMap
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
IdentityHashMap<Integer, String> ihmap
= new IdentityHashMap<Integer, String>();
ihmap.put( 10 , "Geeks" );
ihmap.put( 20 , "for" );
ihmap.put( 30 , "Geeks" );
ihmap.put( 40 , "Welcomes" );
ihmap.put( 50 , "You" );
System.out.println( "IdentityHashMap size : "
+ ihmap.size());
System.out.println( "Initial identity hash map: "
+ ihmap);
Iterator<IdentityHashMap.Entry<Integer, String> >
itr = ihmap.entrySet().iterator();
while (itr.hasNext()) {
IdentityHashMap.Entry<Integer, String> entry
= itr.next();
System.out.println( "Key = " + entry.getKey()
+ ", Value = "
+ entry.getValue());
}
}
}
|
Output
IdentityHashMap size : 5
Initial identity hash map: {10=Geeks, 40=Welcomes, 50=You, 30=Geeks, 20=for}
Key = 10, Value = Geeks
Key = 40, Value = Welcomes
Key = 50, Value = You
Key = 30, Value = Geeks
Key = 20, Value = for
Difference between HashMap and IdentityHashMap in Java
S.NO. |
HashMap |
IdentityHashMap |
1. |
HashMap implements the Map interface but it doesn’t violate the map general contract. |
IdentityHashMap also implements the Map interface but it intentionally violates the map general contract. |
2. |
HashMap uses object equality to compare the key and values. |
IdentityHashMap uses reference equality to compare the key and values. |
3. |
HashMap uses the hashCode() method of HashMap class to find the bucket location. |
IdentityHashMap doesn’t use the hashCode() method instead it uses the System.IdentityHashCode() method to find the bucket location. |
4. |
HashMap uses chaining. |
IdentityHashMap uses a simple liner probe hash table. |
5. |
To safely store the objects in HashMap the keys need to be immutable. |
IdentityHashMap doesn’t require the key to be immutable. |
6. |
HashMap performs slightly less than the IdentityHashMap. |
IdentityHashMap performs better than HashMap. |
Last Updated :
19 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...