Differences and Similarities between HashMap and IdentityHashMap with program in java


In this Collection framework tutorial we will learn what are differences and similarities between java.util.HashMap and java.util.IdentityHashMap in java.



Contents of page :
  • Differences between HashMap and IdentityHashMap in java >
  • Similarity between HashMap and IdentityHashMap in java >
  • Program 1 shows >
    • comparing keys (and values) performs object-equality in place of reference-equality . In an HashMap, two keys k1 and k2 are equal if and only if (k1==null ? k2==null : k1.equals(k2)).
  • Program 2 shows >
    • comparing keys (and values) performs reference-equality in place of object-equality. In an IdentityHashMap, two keys k1 and k2 are equal if and only if (k1==k2)
  • Program 3 shows >
    • overridden equals() and hashCode() method are called when put, get methods are called in HashMap.
  • Program 4 shows >
    • overridden equals() and hashCode() method are not called when put, get methods are called in IdentityHashMap. Because IdentityHashMap implements equals() and hashCode() method by itself and checks for reference-equality of keys.


Differences between java.util.HashMap and java.util.IdentityHashMap in java >

Property
java.util.HashMap
java.util.IdentityHashMap
1
Keys comparison   object-equality  vs reference-equality
HashMap when comparing keys (and values) performs object-equality not reference-equality. In an HashMap, two keys k1 and k2 are equal if and only if (k1==null ? k2==null : k1.equals(k2))
IdentityHashMap when comparing keys (and values) performs reference-equality in place of object-equality. In an IdentityHashMap, two keys k1 and k2 are equal if and only if (k1==k2)
2
Initial size
Constructs a new HashMap, Its initial capacity is 16 in java.
new HashMap();
Constructs a new IdentityHashMap, with maximum size of 21 in java.
new IdentityHashMap();
3
Introduced in which java version
HashMap was introduced in second version of java i.e. JDK 2.0
IdentityHashMap was introduced in fourth version of java i.e. JDK 4.0
4
Program
Program 1 shows >
comparing keys (and values) performs object-equality in place of reference-equality . In an HashMap, two keys k1 and k2 are equal if and only if (k1==null ? k2==null : k1.equals(k2)).
Program 2 shows >
comparing keys (and values) performs reference-equality in place of object-equality. In an IdentityHashMap, two keys k1 and k2 are equal if and only if (k1==k2).
5
overridden equals() and hashCode() method call?
overridden equals() and hashCode() method are called when put, get methods are called in HashMap.

As shown in Program 3.
overridden equals() and hashCode() method are not called when put, get methods are called in IdentityHashMap.
Because IdentityHashMap implements equals() and hashCode() method by itself and checks for reference-equality of keys.

As shown in Program 4.
6
Application - can maintain proxy object
HashMap cannot be used to maintain proxy object.
IdentityHashMap can be used to maintain proxy objects. For example, we might need to maintain proxy object for each object debugged in the program.



So far we have learned what are differences between java.util.HashMap and java.util.IdentityHashMap in java.
Now we will learn similarities between java.util.HashMap and java.util.IdentityHashMap in Collection framework in java.




/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */

Similarity between java.util.HashMap and java.util.IdentityHashMap  >

Property
java.util.HashMap and java.util.IdentityHashMap
1
synchronization
HashMap and IdentityHashMap both are not synchronized (because 2 threads on same Map  object can  access it at same time).
2
Null keys and values
HashMap and IdentityHashMap both allows to store one null key and many null values i.e. many keys can have null value in java.
3
Implements java.util.Map
HashMap and IdentityHashMap both are implementation of the java.util.Map interface in java.
4
Complexity
Complexity offered by methods of HashMap and IdentityHashMap is same.

Operation/ method
Worst case
Best case
put(K key, V value)
O(n)
O(1)
get(Object key)
O(n)
O(1)
5
iterators are fail-fast
The iterators returned by the iterator() method of the HashMap and IdentityHashMap are fail-fast >
  1. map.keySet().iterator()
  2. map.values().iterator()
  3. map.entrySet().iterator()

all three iterators are fail-fast




Example/Program 1 shows >
comparing keys (and values) performs object-equality in place of reference-equality . In an HashMap, two keys k1 and k2 are equal if and only if (k1==null ? k2==null : k1.equals(k2)).
import java.util.HashMap;
import java.util.Map;
public class HashMapExample {
public static void main(String args[]){
  
   Map<String,String> hashMap=new HashMap<String,String>();
  
   hashMap.put(new String("a"), "audi");
   hashMap.put(new String("a"), "ferrari");
   System.out.println(hashMap);
  
}
}
/*OUTPUT
{a=ferrari}
*/
object-equality >
new String("a").equals(new String("a")) returns true.

As HashMap performs object-equality. In above program object-equality is performed. Hence, only one key is stored in map.


Example/Program 2 shows >
comparing keys (and values) performs reference-equality in place of object-equality. In an IdentityHashMap, two keys k1 and k2 are equal if and only if (k1==k2)
import java.util.IdentityHashMap;
import java.util.Map;
public class IdentityHashMapExample {
public static void main(String args[]){
  
   Map<String,String> identityHashMap=new IdentityHashMap<String,String>();
  
   identityHashMap.put(new String("a"), "audi");
   identityHashMap.put(new String("a"), "ferrari");
   System.out.println(identityHashMap);
  
}
}
/*OUTPUT
{a=audi, a=ferrari}
*/
reference-equality >
new String("a") == new String("a") returns false.

As IdentityHashMap performs reference-equality. In above program reference-equality is performed. Hence, two key are stored in map.



Example/ Program 3 shows >
overridden equals() and hashCode() method are called when put, get methods are called in HashMap.
import java.util.HashMap;
import java.util.Map;
class Emp{
   String name;
   Emp(String name){
          this.name=name;
   }
  
   @Override
   public int hashCode(){
          System.out.println("hashCode()");
          return (this.name==null ? 0: this.name.hashCode() );      
   }
   @Override
   public boolean equals(Object obj){
          System.out.println("equals()");
          Emp emp=(Emp)obj;
          return (emp.name==this.name || emp.name.equals(this.name));      
   }
}
public class HashMapExample {
public static void main(String args[]){
  
   Map<Emp,String> hashMap=new HashMap<Emp,String>();
  
   hashMap.put(new Emp("a"), "audi");
   hashMap.put(new Emp("a"), "bmw");
   System.out.println(">>>>>>>>>>>>>> size = "+hashMap.size());
   System.out.println(">>>>>>>>>>>>>> hashMap.get(new Emp(\"a\")) = "
                           +hashMap.get(new Emp("a")));
  
}
}
/*OUTPUT
hashCode()
hashCode()
equals()
>>>>>>>>>>>>>> size = 1
hashCode()
equals()
>>>>>>>>>>>>>> hashMap.get(new Emp("a")) = bmw
*/



Example/Program 4 shows >
overridden equals() and hashCode() method are not called when put, get methods are called in IdentityHashMap. Because IdentityHashMap implements equals() and hashCode() method by itself and checks for reference-equality of keys.
import java.util.IdentityHashMap;
import java.util.Map;
class Emp{
   String name;
   Emp(String name){
          this.name=name;
   }
  
   @Override
   public int hashCode(){
          System.out.println("in hashCode()");
          return (this.name==null ? 0: this.name.hashCode() );      
   }
   @Override
   public boolean equals(Object obj){
          System.out.println("in equals()");
          Emp emp=(Emp)obj;
          return (emp.name==this.name || emp.name.equals(this.name));      
   }
}
public class IdentityHashMapExample{
public static void main(String args[]){
  
   Map<Emp,String> identityHashMap=new IdentityHashMap<Emp,String>();
  
   identityHashMap.put(new Emp("a"), "audi");
   identityHashMap.put(new Emp("a"), "bmw");
   System.out.println(">>>>>>>>>>>>>> size = "+identityHashMap.size());
   System.out.println(">>>>>>>>>>>>>> identityHashMap.get(new Emp(\"a\")) = "
                            +identityHashMap.get(new Emp("a")));
  
}
}
/*OUTPUT
>>>>>>>>>>>>>> size = 2
>>>>>>>>>>>>>> identityHashMap.get(new Emp("a")) = null
*/
In above program get method performed reference-equality of keys, hence returned false.


So in this Collection framework tutorial we learned what are differences and similarities between java.util.HashMap and java.util.IdentityHashMap in java.

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>


Important Similarity and Differences >

HashMap and Hashtable - Similarity and Differences in java

HashMap and ConcurrentHashMap - Similarity and Differences in java

TreeMap vs ConcurrentSkipListMap - Similarity and Differences with program in java




Map hierarchy tutorial in java - Detailed - java.util.HashMap, java.util.Hashtable, java.util.concurrent.ConcurrentHashMap, java.util.LinkedHashMap, java.util.TreeMap, java.util.concurrent.ConcurrentSkipListMap, java.util.IdentityHashMap, java.util.WeakHashMap, java.util.EnumMap classes


No comments:

Post a Comment