Saturday, November 23, 2019

Difference Betwixt Identityhashmap, Weakhashmap Together With Enummap Inwards Java

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

Now that you lot know the basic deviation betwixt IdentityHashMap, EnumMap, as well as WeakHashMap, let's larn picayune to a greater extent than almost their unique features to sympathize them better. It's non necessary that you lot own got to usage it but if you lot know how they locomote as well as when they are used volition assistance you lot to sympathize existing code as well as assistance inwards troubleshooting as well as debugging.



IdentityHashMap vs WeakHashMap vs EnumMap inwards Java

As I told each of these are special Map implementation classes as well as has unique uses. In gild to larn when to usage them, you lot must larn the unique characteristic they offering as well as their deviation amongst full general purposes Map implementations similar HashMap, LinkedHashMapand Hashtable.




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

Since == operator solely furnish truthful if the reference variables signal to the same object, it's non possible to acquire the value amongst a dissimilar object fifty-fifty if it seem equal inwards all field. You must concur the reference of the key object outside the IdentityHashMap to acquire the value back.

Another trial of using == operator instead of equals() method is that in that location would locomote 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 almost the performance conduct on of collisions inwards HashMap.


2) The substitution deviation betwixt WeakHashMap as well as other Map classes similar HashMap, IdentityHashMap as well as EnumMap is that its keys are WeakReferences, which way both key as well as 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 equally Cache inwards retentiveness constraint surround because Map itself volition accept aid of removing unused Mapping. On the other hand, this tin elbow grease odd demeanor if afterwards removed key is passed from the dissimilar role of a program. See Core Java Volume 1 - Fundamentals past times Cay S. Horstmann to larn to a greater extent than almost WeakHashMap as well as its practical usage.


3) The tertiary one, EnumMap is a special Map implementations for Enum keys, this is too the substitution deviation betwixt EnumMap as well as simply about other full general purpose Map degree e.g. HashMap or Hashtable. Unlike others EnumMap solely allows Enum constants to locomote used equally keys. If you lot endeavour to shop keys other than Enum than compiler volition throw an error.

It's a special implementation for Enums so it takes payoff of key universe i.e. release of Enum constants as well as creates the backup array amongst the same size equally of key universe. See Java Performance Companion by Charlie Hunt  to sympathize performance practise goodness of EnumMap inwards Java application.

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



When to usage EnumMap, WeakHashMap, as well as IdentityHashMap inwards Java

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

Use EnumMap if you lot are using Enum constants equally keys. It volition in all probability accept less retentiveness as well as give improve performance than HashMap or whatever other full general purpose Map. It too has a skillful gamble to locomote upgraded or replaced past times simply about other to a greater extent than performant implementation.

You tin usage WeakHashMap if you lot are storing mapping inwards a retentiveness constraint surround as well as you lot are ok amongst keys disappearing suddenly. Since WeakHashMap uses keys amongst WeakReference they locomote past times eligible for GC in i trial the external reference to the key is removed. It's oftentimes used equally Cache inwards Java.  You tin farther read Big Java: Early Objects fifth Edition to larn to a greater extent than almost these specialized classes of Java Collection framework.

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

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

That's all almost the difference betwixt IdentityHashMap, WeakHashMap, EnumMap as well as other full general purpose Map implementations inwards Java. Just recollect their unique characteristic as well as when to usage them. Most probable you lot volition non usage them on a daily dry soil but 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 as well as HashSet inwards Java? (answer)
  • Difference betwixt TreeMap as well as TreeSet inwards Java? (answer)
  • Difference betwixt HashMap as well as ConcurrentHashMap inwards Java? (answer)
  • Difference betwixt HashMap as well as LinkedHashMap inwards Java? (answer)
  • Difference betwixt Hashtable as well as HashMap inwards Java? (answer)
  • Difference betwixt HashSet as well as TreeSet inwards Java? (answer)
  • Difference betwixt ArrayList as well as LinkedList inwards Java? (answer)
  • Difference betwixt Vector as well as ArrayList inwards Java? (answer)
  • Difference betwixt EnumMap as well as HashMap inwards Java

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java

No comments:

Post a Comment