Difference Betwixt Enummap Too Hashmap Inwards Java

HashMap vs EnumMap inwards Java
What is the divergence betwixt EnumMap together with HashMap inwards Java is the latest Java collection interview question which has been asked to a distich of my friends? This is i of the tricky Java questions, specially if you lot are non real much familiar amongst EnumMap inwards Java, which is non uncommon, given you lot tin work it amongst simply Enum keys. The primary difference betwixt EnumMap together with HashMap is that EnumMap is a specialized Map implementation solely for Enum every bit key. Using Enum every bit cardinal allows doing unopen to implementation marking optimization for high functioning which is to a greater extent than oft than non not possible amongst other objects every bit key. We cause got seen a lot of interview questions on HashMap inwards our article How HashMap industrial plant inwards Java but what nosotros missed at that topographic point is this inquiry which is lately asked to unopen to of my friends. Unlike HashMap, EnumMap is non applicable for every instance but it's best suited when you lot cause got Enum every bit key. We cause got already covered basics of EnumMap together with unopen to EnumMap illustration inwards my terminal article What is EnumMap inwards Java together with In this post, nosotros volition focus on cardinal differences betwixt HashMap together with EnumMap inwards Java.

EnumMap vs HashMap

Before looking differences betwixt EnumMap together with HashMap, few words nearly What is mutual betwixt them. Both of them implements Map interface so they tin live on used inwards all methods which bring Map together with information tin live on accessed using mutual Map methods e.g. get() together with put(). Internally EnumMap is represented using Array together with provides constant fourth dimension functioning for mutual methods e.g. get() or put(). Now let's encounter few differences betwixt EnumMap vs HashMap :


1) As said earlier, commencement together with initiative of all divergence betwixt EnumMap together with HashMap is that EnumMap is optimized for enum keys acre HashMap is a full general work Map implementation similar to Hashtable. you lot tin non work whatever type other than Enum every bit cardinal inwards EnumMap but you lot tin work both Enum together with whatever other Object every bit cardinal inwards HashMap.

2) Another divergence betwixt EnumMap together with HashMap is performance. every bit discussed inwards the previous point, due to specialized optimization done for Enum keys, EnumMap is probable to perform ameliorate than HashMap when using enum every bit cardinal object.

3) One to a greater extent than affair which tin live on considered every bit the divergence betwixt HashMap together with EnumMap is the probability of Collision. Since Enum is internally maintained every bit array together with they are stored inwards their natural lodge using ordinal(), every bit shown inwards next code which is taken from put() method of EnumMap

    int index = ((Enum)key).ordinal();
    Object oldValue = vals[index];
    vals[index] = maskNull(value);

Since EnumMap doesn't telephone phone hashCode method on keys, at that topographic point is no adventure of collision.

These were unopen to notable difference betwixt EnumMap together with HashMap inwards Java. In curt EnumMap is best suited for enum keys, for which it has optimized together with performed ameliorate than HashMap inwards Java. Use EnumMap whenever you lot tin work enum every bit keys.
What is the divergence betwixt EnumMap together with HashMap inwards Java is the  Difference betwixt EnumMap together with HashMap inwards Java

Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt Hashtable together with ConcurrentHashMap inwards Java


Sumber https://javarevisited.blogspot.com/

0 Response to "Difference Betwixt Enummap Too Hashmap Inwards Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel