Difference between HashMap and IndentityHashMap

It is exactly same as HashMap except the below difference.

  • HashMap uses equals() method to indentify duplicate keys for object’s content comparision.
  • IndentityHashMap uses ‘==’ operator to identify duplicate keys.

Example : 

In this example we are creating HashMap and IndentityHashMap objects and inserting integer values.

 

 Output:

HashMap uses equals method for comparison (i1.equals (i2)).

IndentityHashMap uses == operator for comparison (i3==i4).

Posted in collections and tagged , , .

Leave a Reply

Your email address will not be published. Required fields are marked *