JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Difference between IdentityHashMap, WeakHashMap and EnumMap in Java

IdentityHashMap vs WeakHashMap vs EnumMap in Java

1) The fundamental difference between IdentityHashMap and other Map implementations e.g. HashMap, Hashtable, WeakHashMap or EnumMap it uses equality operator (==) to search and get the value back. If you know how to get method of Map works the know that other Map implementation, which uses equals() method of the key object for that purpose.

Since == operator only return true if the reference variables point to the same object, it's not possible to get the value with a different object even if it appear equal in all field. You must hold the reference of the key object outside the IdentityHashMap to get the value back.

Another consequence of using == operator instead of equals() method is that there would be less collision compared to other Map implementations e.g. HsahMap. See Java Performance The Definitive Guide By Scott Oaks to learn more about the performance impact of collisions in HashMap.


2) The fundamental difference between WeakHashMap and other Map classes like HashMap, IdentityHashMap and EnumMap is that its keys are WeakReferences, which means both key and value becomes eligible to garbage collection if a key is no longer referenced from elsewhere in the program.

This property makes WeakHashMap a good candidate for using as Cache in memory constraint environment because Map itself will take care of removing unused Mapping. On the other hand, this can cause unusual behavior if later removed key is passed from the different part of a program. See Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about WeakHashMap and its practical usage.


3) The third one, EnumMap is a special Map implementations for Enum keys, this is also the fundamental difference between EnumMap and another general purpose Map class e.g. HashMap or Hashtable. Unlike others EnumMap only allows Enum constants to be used as keys. If you try to store keys other than Enum than compiler will throw an error.

It's a special implementation for Enums hence it takes advantage of key universe i.e. number of Enum constants and creates the backup array with the same size as of key universe. See Java Performance Companion by Charlie Hunt  to understand performance benefit of EnumMap in Java application.


Difference between IdentityHashMap, WeakHashMap and EnumMap in Java

When to use EnumMap, WeakHashMap, and IdentityHashMap in Java
Now that you know their special feature, it's easy to decide when to use them. Use IdentityHashMap if use needs strict equality check by using == operator. It's not a general purpose Map because it doesn't use equals() method for comparing keys, hence also breaks Map interface's contract.

Use EnumMap if you are using Enum constants as keys. It will probably take less memory and give better performance than HashMap or any other general purpose Map. It also has a good chance to be upgraded or replaced by another more performant implementation.

You can use WeakHashMap if you are storing mapping in a memory constraint environment and you are ok with keys disappearing suddenly. Since WeakHashMap uses keys with WeakReference they become eligible for GC once the external reference to the key is removed. It's often used as Cache in Java.  You can further read Big Java: Early Objects 5th Edition to learn more about these specialized classes of Java Collection framework.

Anyway, here is a nice summary of their performance for common operations like storing mapping, retrieving mapping, and searching keys and values:

Difference between IdentityHashMap, WeakHashMap and EnumMap in Java

Difference between IdentityHashMap, WeakHashMap and EnumMap in Java

.