Difference between IdentityHashMap, WeakHashMap and EnumMap in Java? Answer

Apart from popular implementation like HashMap and LinkedHashMap, java.util.Map also has some specialized implementation classes e.g. IdentifyHashMap which uses == instead of equals() method for comparing keys, WeakHashMap which uses WeakReference to wrap the key object and a key/value mapping is removed when the key is no longer referenced from elsewhere, and EnumMap where keys are Enum constants. Many Java developer doesn't know about these special Map implementation classes and failed to take advantage of improved performance and feature offered by them. 

Their knowledge is also important from the core Java interview perspective because the Java interviewer put a huge focus on JDK API, particularly the Java Collection framework and Java Concurrency framework which includes concurrent collections classes.

Now that you know the basic difference between IdentityHashMap, EnumMap, and WeakHashMap, let's learn a little more about their unique features to understand them better. It's not necessary that you have to use it but if you know how they work and when they are used will help you to understand existing code and help in troubleshooting and debugging.



IdentityHashMap vs WeakHashMap vs EnumMap in Java

As I told each of these are special Map implementation classes and has unique uses. In order to learn when to use them, you must learn the unique feature they offer and their difference with general purposes Map implementations like HashMap, LinkedHashMapand Hashtable.

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

Since == operator only returns 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 appears equal in all fields. You must hold the reference of the key object outside the IdentityHashMap to get the value back.

Another consequence of using the == operator instead of the equals() method is that there would be less collision compared to other Map implementations like HsahMap. You can also see these Java performance courses to learn more about the performance impact of collisions in HashMap.


2. WeakRefrence Keys
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 a memory constraint environment because Map itself will take care of removing unused Mapping. On the other hand, this can cause unusual behavior if the later removed key is passed from the different parts of a program. See Core Java Volume 1 - Fundamentals by Cay S. Horstmann to learn more about WeakHashMap and its practical usage.


3. Usage
The third one, EnumMap is a special Map implementation for Enum keys, this is also the fundamental difference between EnumMap and another general-purpose Map class like HashMap or Hashtable. Unlike others EnumMap only allows Enum constants to be used as keys. If you try to store keys other than Enum then the 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 the performance benefit of EnumMap in Java application.




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 the equals() method for comparing keys, hence also breaks the 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 join these Java Collection courses to learn more about these specialized classes of the 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

That's all about the difference between IdentityHashMap, WeakHashMap, EnumMap, and other general-purpose Map implementations in Java. Just remember their unique feature and when to use them. Most likely you will not use them on a daily basis but when the situation arises you will appreciate them for their unique offering.

Other Java Collection Interview Questions you may like
  • Difference between ArrayList and HashSet in Java? (answer)
  • Difference between TreeMap and TreeSet in Java? (answer)
  • Difference between HashMap and ConcurrentHashMap in Java? (answer)
  • Difference between HashMap and LinkedHashMap in Java? (answer)
  • Difference between Hashtable and HashMap in Java? (answer)
  • Difference between HashSet and TreeSet in Java? (answer)
  • Difference between ArrayList and LinkedList in Java? (answer)
  • Difference between Vector and ArrayList in Java? (answer)
  • Difference between EnumMap and HashMap in Java

Thanks for reading this article so far. If you found the difference between IdentityHashMap, WeakHashMap, and EnumMap in Java useful then please share with your friends and colleagues. If you have any questions or feedback, please ask. 

1 comment:

Feel free to comment, ask questions if you have any doubt.