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
// 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); } } |
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
// 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()); } } } |
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. |
Please Login to comment...