Difference betwixt IdentityHashMap, WeakHashMap together with EnumMap inwards Java

Apart from pop implementation similar HashMap too LinkedHashMap, java.util.Map likewise has closed to specialized implementation classes e.g. IdentifyHashMap which uses == instead of equals() method for comparison keys, WeakHashMap which uses WeakReference to roll the key object too a key/value mapping is removed when the key is no longer referenced from elsewhere, too EnumMap where keys are Enum constants. Many Java developer doesn't know close these special Map implementation classes too failed to accept payoff of improved performance too characteristic offered past times them. Their cognition is likewise of import from gist Java interview perspective because Java interviewer set a huge focus on JDK API, specially Java Collection framework too Java Concurrency framework which includes concurrent collections classes.

Now that you lot know the basic departure betwixt IdentityHashMap, EnumMap, too WeakHashMap, let's larn piddling to a greater extent than close their unique features to empathise them better. It's non necessary that you lot own got to usage it only if you lot know how they operate too when they are used volition assistance you lot to empathise existing code too assistance inwards troubleshooting too debugging.



IdentityHashMap vs WeakHashMap vs EnumMap inwards Java

As I told each of these are special Map implementation classes too has unique uses. In social club to larn when to usage them, you lot must larn the unique characteristic they offering too their departure alongside full general purposes Map implementations similar HashMap, LinkedHashMapand Hashtable.



1) The key departure betwixt IdentityHashMap too other Map implementations e.g. HashMap, Hashtable, WeakHashMap or EnumMap it uses equality operator (==) to search too acquire the value back. If you lot know how to acquire method of Map plant the know that other Map implementation, which uses equals() method of the key object for that purpose.

Since == operator entirely furnish truthful if the reference variables indicate to the same object, it's non possible to acquire the value alongside a dissimilar object fifty-fifty if it look equal inwards all field. You must concord the reference of the key object outside the IdentityHashMap to acquire the value back.

Another effect of using == operator instead of equals() method is that at that topographic point would last less collision compared to other Map implementations e.g. HsahMap. See Java Performance The Definitive Guide By Scott Oaks to larn to a greater extent than close the performance send upon of collisions inwards HashMap.


2) The key departure betwixt WeakHashMap too other Map classes similar HashMap, IdentityHashMap too EnumMap is that its keys are WeakReferences, which agency both key too value becomes eligible to garbage collection if a key is no longer referenced from elsewhere inwards the program.

This belongings makes WeakHashMap a skillful candidate for using every bit Cache inwards retention constraint environs because Map itself volition accept attention of removing unused Mapping. On the other hand, this tin drive odd demeanor if later on removed key is passed from the dissimilar purpose of a program. See Core Java Volume 1 - Fundamentals past times Cay S. Horstmann to larn to a greater extent than close WeakHashMap too its practical usage.


3) The 3rd one, EnumMap is a special Map implementations for Enum keys, this is likewise the key departure betwixt EnumMap too closed to other full general purpose Map course of written report e.g. HashMap or Hashtable. Unlike others EnumMap entirely allows Enum constants to last used every bit keys. If you lot endeavor to shop keys other than Enum than compiler volition throw an error.

It's a special implementation for Enums thus it takes payoff of key universe i.e. publish of Enum constants too creates the backup array alongside the same size every bit of key universe. See Java Performance Companion by Charlie Hunt  to empathise performance practice goodness of EnumMap inwards Java application.

 Apart from pop implementation similar  Difference betwixt IdentityHashMap, WeakHashMap too EnumMap inwards Java



When to usage EnumMap, WeakHashMap, too IdentityHashMap inwards Java

Now that you lot know their special feature, it's slow to create upwards one's hear when to usage them. Use IdentityHashMap if usage needs strict equality banking concern fit past times using == operator. It's non a full general purpose Map because it doesn't usage equals() method for comparison keys, thus likewise breaks Map interface's contract.

Use EnumMap if you lot are using Enum constants every bit keys. It volition in all likelihood accept less retention too give ameliorate performance than HashMap or whatsoever other full general purpose Map. It likewise has a skillful risk to last upgraded or replaced past times closed to other to a greater extent than performant implementation.

You tin usage WeakHashMap if you lot are storing mapping inwards a retention constraint environs too you lot are ok alongside keys disappearing suddenly. Since WeakHashMap uses keys alongside WeakReference they pop off eligible for GC in 1 lawsuit the external reference to the key is removed. It's oftentimes used every bit Cache inwards Java.  You tin farther read Big Java: Early Objects fifth Edition to larn to a greater extent than close these specialized classes of Java Collection framework.

Anyway, hither is a overnice summary of their performance for mutual operations similar storing mapping, retrieving mapping, too searching keys too values:

 Apart from pop implementation similar  Difference betwixt IdentityHashMap, WeakHashMap too EnumMap inwards Java

That's all close the difference betwixt IdentityHashMap, WeakHashMap, EnumMap too other full general purpose Map implementations inwards Java. Just recollect their unique characteristic too when to usage them. Most probable you lot volition non usage them on a daily Earth only when province of affairs arise you lot volition appreciate them for their unique offering.

Other Java Collection Interview Questions you lot may like
  • Difference betwixt ArrayList too HashSet inwards Java? (answer)
  • Difference betwixt TreeMap too TreeSet inwards Java? (answer)
  • Difference betwixt HashMap too ConcurrentHashMap inwards Java? (answer)
  • Difference betwixt HashMap too LinkedHashMap inwards Java? (answer)
  • Difference betwixt Hashtable too HashMap inwards Java? (answer)
  • Difference betwixt HashSet too TreeSet inwards Java? (answer)
  • Difference betwixt ArrayList too LinkedList inwards Java? (answer)
  • Difference betwixt Vector too ArrayList inwards Java? (answer)
  • Difference betwixt EnumMap too HashMap inwards Java

Further Learning
Java Fundamentals: Collections
From Collections to Streams inwards Java 8 Using Lambda Expressions
What's New inwards Java 8

Subscribe to receive free email updates:

0 Response to "Difference betwixt IdentityHashMap, WeakHashMap together with EnumMap inwards Java"

Posting Komentar