Difference Betwixt Enummap As Well As Hashmap Inwards Java

HashMap vs EnumMap inwards Java
What is the deviation betwixt EnumMap in addition to HashMap inwards Java is the latest Java collection interview question which has been asked to a dyad of my friends? This is 1 of the tricky Java questions, peculiarly if you lot are non rattling much familiar alongside EnumMap inwards Java, which is non uncommon, given you lot tin work it alongside exactly Enum keys. The principal difference betwixt EnumMap in addition to HashMap is that EnumMap is a specialized Map implementation solely for Enum equally key. Using Enum equally fundamental allows doing roughly implementation marker optimization for high functioning which is to a greater extent than oft than non not possible alongside other objects equally key. We stimulate got seen a lot of interview questions on HashMap inwards our article How HashMap industrial plant inwards Java but what nosotros missed at that spot is this interrogation which is late asked to roughly of my friends. Unlike HashMap, EnumMap is non applicable for every instance but it's best suited when you lot stimulate got Enum equally key. We stimulate got already covered basics of EnumMap in addition to roughly EnumMap instance inwards my terminal article What is EnumMap inwards Java in addition to In this post, nosotros volition focus on fundamental differences betwixt HashMap in addition to EnumMap inwards Java.

EnumMap vs HashMap

Before looking differences betwixt EnumMap in addition to HashMap, few words close What is mutual betwixt them. Both of them implements Map interface so they tin hold out used inwards all methods which bring Map in addition to information tin hold out accessed using mutual Map methods e.g. get() in addition to put(). Internally EnumMap is represented using Array in addition to provides constant fourth dimension functioning for mutual methods e.g. get() or put(). Now let's run into few differences betwixt EnumMap vs HashMap :


1) As said earlier, commencement in addition to initiative of all deviation betwixt EnumMap in addition to HashMap is that EnumMap is optimized for enum keys piece HashMap is a full general role Map implementation similar to Hashtable. you lot tin non work whatever type other than Enum equally fundamental inwards EnumMap but you lot tin work both Enum in addition to whatever other Object equally fundamental inwards HashMap.

2) Another deviation betwixt EnumMap in addition to 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 hold out considered equally the deviation betwixt HashMap in addition to EnumMap is the probability of Collision. Since Enum is internally maintained equally array in addition to 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 spot is no direct a opportunity of collision.

These were roughly notable difference betwixt EnumMap in addition to HashMap inwards Java. In curt EnumMap is best suited for enum keys, for which it has optimized in addition to performed improve than HashMap inwards Java. Use EnumMap whenever you lot tin work enum equally keys.
What is the deviation betwixt EnumMap in addition to HashMap inwards Java is the  Difference betwixt EnumMap in addition to HashMap inwards Java

Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt Hashtable in addition to ConcurrentHashMap inwards Java

Belum ada Komentar untuk "Difference Betwixt Enummap As Well As Hashmap Inwards Java"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel