Difference Betwixt Enummap As Well As Hashmap Inward Java

Advertisement

Masukkan script iklan 970x90px

Difference Betwixt Enummap As Well As Hashmap Inward Java

Sabtu, 18 Juli 2020

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 twosome of my friends? This is i of the tricky Java questions, peculiarly if you lot are non really much familiar amongst EnumMap inwards Java, which is non uncommon, given you lot tin flame usage it amongst but Enum keys. The principal difference betwixt EnumMap together with HashMap is that EnumMap is a specialized Map implementation alone for Enum equally key. Using Enum equally fundamental allows doing around implementation degree optimization for high functioning which is to a greater extent than oftentimes than non not possible amongst other objects equally key. We induce 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 late asked to around of my friends. Unlike HashMap, EnumMap is non applicable for every instance but it's best suited when you lot induce got Enum equally key. We induce got already covered basics of EnumMap together with around EnumMap illustration inwards my terminal article What is EnumMap inwards Java together with In this post, nosotros volition focus on fundamental differences betwixt HashMap together with EnumMap inwards Java.

EnumMap vs HashMap

Before looking differences betwixt EnumMap together with HashMap, few words close What is mutual betwixt them. Both of them implements Map interface so they tin flame move used inwards all methods which induce got Map together with information tin flame move 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, outset together with firstly divergence betwixt EnumMap together with HashMap is that EnumMap is optimized for enum keys piece HashMap is a full general usage Map implementation similar to Hashtable. you lot tin flame non usage whatever type other than Enum equally fundamental inwards EnumMap but you lot tin flame usage both Enum together with whatever other Object equally fundamental inwards HashMap.

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

3) One to a greater extent than affair which tin flame move considered equally the divergence betwixt HashMap together with EnumMap is the probability of Collision. Since Enum is internally maintained equally array together with they are stored inwards their natural guild using ordinal(), equally 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 telephone hashCode method on keys, at that topographic point is no jeopardy of collision.

These were around 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 improve than HashMap inwards Java. Use EnumMap whenever you lot tin flame usage enum equally 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