Senin, 23 Juni 2014

Difference between IdentityHashMap and HashMap in Java




IdentityHashMap in Java was added in Java 1.4 but still its one of those
lessor known class in Java. Main difference between IdentityHashMap and HashMap
in Java is that
IdentityHashMap is a special implementation of
Map interface which doesn't use equals() and hashCode() method for
comparing object unlike other implementation of
Map e.g. HashMap. Instead IdentityHashMap use equality
operator
"=="  to compare keys and values in Java which makes
it faster compare to HashMap and suitable where you need reference equality
check and instead of logical equality. By the way
IdentityHashMap is special
implementation of Map interface much like EnumMap but it also violates
general contract of Map interface which mandates using equals method for
comparing Object. Also
IdentityHashMap vs HashMap is a good Java question and have
been asked couple of times.  Though this
question is not as popular as How HashMap works in Java or Difference between Hashtable and HashMap,
it’s still a good question to ask. In this Java tutorial
we will see example of
IdentityHashMap and explores some key differences between IdentityHashMap and HashMap in Java.




Difference between IdentityHashMap and
HashMap



Difference between IdentityHashmap vs HashMap in Java Interview questionThough both HashMap and IdentityHashMap implements Map interface, have
fail-fast Iterator and non synchronized collections, following are some key differences between HashMap and
IdentityHashMap in Java
.








1) Main difference between HashMap
vs IdentityHashMap
is that IdentityHashMap uses equality operator
"==" for comparing keys and values inside Map while HashMap uses equals method for comparing
keys and values.





2) Unlike HashMap, who uses hashcode to find bucket location, IdentityHashMap also
doesn't use
hashCode() instead it uses System.identityHashCode(object).





3) Another key difference between IdentityHashMap and HashMap in Java is
Speed. Since
IdentityHashMap doesn't use equals() its
comparatively faster than
HashMap for object with expensive equals() and hashCode().





4) One more difference between HashMap and IdentityHashMap is
Immutability of key. One of the basic requirement to safely store Objects in HashMap is keys
needs to be immutable,
IdentityHashMap doesn't require keys to be
immutable as it is not relied on equals and hashCode.





There is also a class called IdentityHashtable which is
analogous to Hashtable in Java but it’s not
part of standard JDK and available in
com.sun... package.





Example
of
IdentityHashMap in Java


Here is an example of IdentityHashMap in Java
which shows key difference between HashMap and IdentityHashMap in comparing
Objects.  
IdentityHashMap uses
equality operator for comparison i instead of equals method in Java :






import java.util.IdentityHashMap;



/**

 * Java program to show difference between HashMap and IdentityHashMap in
Java

 * @author Javin Paul

 */


public abstract class Testing {



   
public static void main(String args[]) {

        IdentityHashMap
<String, String> identityMap = new IdentityHashMap<String, String>();

     

        identityMap.
put("sony", "bravia");

        identityMap.
put(new String("sony"), "mobile");

     

       
//size of identityMap should be 2 here because two
strings are different objects


        System.
out.println("Size of IdentityHashMap:
"

+ identityMap.
size());

        System.
out.println("IdentityHashMap: " + identityMap);

     

        identityMap.
put("sony", "videogame");

     

         
//size of identityMap still should be
2 because "sony" and "sony" is same object


        System.
out.println("Size of IdentityHashMap:
"

+ identityMap.
size());

        System.
out.println("IdentityHashMap: " + identityMap);

   

   
}

}



Output

Size of IdentityHashMap:
2

IdentityHashMap:
{sony=bravia, sony=mobile}

Size of IdentityHashMap:
2

IdentityHashMap:
{sony=videogame, sony=mobile}









That’s all on difference between IdentityHashMap and HashMap in Java.  As I said IdentityHashMap violates Map
interface general contract and should only be used when reference equality make
sense. As per Javadoc, IdentityHashMap is suitable to keep object reference
during Serialization and deep copy and can also be used  to maintain as proxy object.





Other useful Java collection tutorials from Javarevisited Blog




























Source:http://javarevisited.blogspot.com/2013/01/difference-between-identityhashmap-and-hashmap-java.html

1 komentar:

  1. Good explanation. Thanks for sharing.
    https://www.flowerbrackets.com/difference-between-equal-operator-vs-equals-method-java/

    BalasHapus