Difference between HashMap and IdentityHashMap in Java
Last Updated :
19 Jan, 2021
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
// Java program to illustrate
// the working of Java HashMap
// to demonstrate
// internal working difference between them
// Importing HashMap class from
// java.util package
import java.util.HashMap;
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty HashMap object
HashMap<Integer, String> map = new HashMap<>();
// Add elements to the map
// Custom inputs
map.put(10, "Geeks");
map.put(20, "for");
map.put(30, "geeks");
map.put(40, "welcome");
map.put(50, "you");
// Printing the size of map
System.out.println("Size of map is:- "
+ map.size());
// Printing the HashMap content
System.out.println("HashMap content: " + map);
// Removing a key 50
map.remove(50);
// Printing the HashMap after the removal
System.out.println("HashMap after removal : "
+ map);
}
}
OutputSize 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
// Java program to illustrate
// working of IdentityHashmap
// to demonstrate
// internal working difference between them
// Importing all classes of
// java.util package
import java.util.*;
// Class for iterating IdentityHashMap
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty IdentityHashMap object
IdentityHashMap<Integer, String> ihmap
= new IdentityHashMap<Integer, String>();
// Mapping string values to int keys
// Custom inputs --> Custom mappings
ihmap.put(10, "Geeks");
ihmap.put(20, "for");
ihmap.put(30, "Geeks");
ihmap.put(40, "Welcomes");
ihmap.put(50, "You");
// Display the size of IdentityHashMap
System.out.println("IdentityHashMap size : "
+ ihmap.size());
// Display the IdentityHashMap
System.out.println("Initial identity hash map: "
+ ihmap);
// Create an Iterator over the IdentityHashMap
Iterator<IdentityHashMap.Entry<Integer, String> >
itr = ihmap.entrySet().iterator();
// Condition check using hasNext() method()
// Condition check using hasNext() method holding
// true if there is any next element remaining
while (itr.hasNext()) {
// next() method which is used to
// retrieve the next element
IdentityHashMap.Entry<Integer, String> entry
= itr.next();
// Print and display key and value pairs
// using getKey() method
System.out.println("Key = " + entry.getKey()
+ ", Value = "
+ entry.getValue());
}
}
}
OutputIdentityHashMap 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. |
Similar Reads
Differences between HashMap and HashTable in Java HashMap and Hashtable store key and value pairs in a hash table. When using a Hashtable or HashMap, we specify an object that is used as a key and the value that you want to be linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored w
3 min read
Difference between HashMap and HashSet HashSet is an implementation of Set Interface which does not allow duplicate value. The main thing is, objects that are stored in HashSet must override equals() for check for equality, and hashCode() methods for no duplicate value are stored in our set. HashMap is an implementation of Map Interface,
4 min read
Differences between TreeMap, HashMap and LinkedHashMap in Java Prerequisite: HashMap and TreeMap in Java TreeMap, HashMap and LinkedHashMap: What's Similar? All offer a key->value map and a way to iterate through the keys. The most important distinction between these classes is the time guarantees and the ordering of the keys.All three classes HashMap, TreeM
5 min read
Difference Between EnumMap and HashMap EnumMap and HashMap both are the classes that implement the Map interface. But there are some differences that exist between them. So we have tried to list out the differences between EnumMap and HashMap. 1. EnumMap: EnumMap is a specialized implementation of the Map interface for enumeration types.
4 min read
Difference between ArrayList and HashSet in Java Here are couple of differences between ArrayList and HashSet. Inheritance: Implementation: Implementation : ArrayList implements List interface while HashSet implements Set interface in Java.Internal implementation: ArrayList is backed by an Array while HashSet is backed by an HashMap.Duplicates : A
3 min read
What is the difference between Hashing and Hash Tables? What is Hashing? Hashing refers to the process of generating a fixed-size output from an input of variable size using the mathematical formulas known as hash functions. This technique determines an index or location for the storage of an item in a data structure. It might not be strictly related to
2 min read
Difference Between Hashtable and Synchronized Map in Java The Hashtable class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a Hashtable, the objects used as keys must implement the hashCode method and also the equals method. Features of Hashtable:
3 min read
Difference between List, Set and Map in Java List interface in Java is a sub-interface of the Java collections interface. It contains the index-based methods to insert, update, delete, and search the elements. It can have duplicate elements also. We can also store the null elements in the list. List preserves the insertion order, it allows pos
4 min read
IdentityHashMap class in Java The IdentityHashMap implements Map interface using Hashtable, using reference-equality in place of object-equality when comparing keys (and values). This class is not a general-purpose Map implementation. While this class implements the Map interface, it intentionally violates Map's general contract
13 min read
Difference Between keySet() and entrySet() Method in Java Map Map Interface is present in Java.util package, which provides mainly three methods KeySet(),entrySet() and values(). These methods are used to retrieve the keys of the map, key-value pairs of the map, and values of the map respectively. Since these methods are part of Map Interface, so we can use th
4 min read