Difference Betwixt Enummap In Addition To Hashmap Inwards Java

HashMap vs EnumMap inwards Java
What is the divergence betwixt EnumMap as well as HashMap inwards Java is the latest Java collection interview question which has been asked to a span of my friends? This is ane of the tricky Java questions, specially if y'all are non real much familiar alongside EnumMap inwards Java, which is non uncommon, given y'all tin utilization it alongside simply Enum keys. The primary difference betwixt EnumMap as well as HashMap is that EnumMap is a specialized Map implementation solely for Enum every bit key. Using Enum every bit cardinal allows doing around implementation score optimization for high functioning which is by as well as large non possible alongside other objects every bit key. We guide hold 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 query which is late asked to around of my friends. Unlike HashMap, EnumMap is non applicable for every instance but it's best suited when y'all guide hold Enum every bit key. We guide hold already covered basics of EnumMap as well as around EnumMap instance inwards my concluding article What is EnumMap inwards Java as well as In this post, nosotros volition focus on cardinal differences betwixt HashMap as well as EnumMap inwards Java.

EnumMap vs HashMap

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


1) As said earlier, starting fourth dimension as well as firstly divergence betwixt EnumMap as well as HashMap is that EnumMap is optimized for enum keys acre HashMap is a full general role Map implementation similar to Hashtable. y'all tin non utilization whatever type other than Enum every bit cardinal inwards EnumMap but y'all tin utilization both Enum as well as whatever other Object every bit cardinal inwards HashMap.

2) Another divergence betwixt EnumMap as well as 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 survive considered every bit the divergence betwixt HashMap as well as EnumMap is the probability of Collision. Since Enum is internally maintained every bit array as well as 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 telephone hashCode method on keys, at that spot is no gamble of collision.

These were around notable difference betwixt EnumMap as well as HashMap inwards Java. In brusk EnumMap is best suited for enum keys, for which it has optimized as well as performed ameliorate than HashMap inwards Java. Use EnumMap whenever y'all tin utilization enum every bit keys.
What is the divergence betwixt EnumMap as well as HashMap inwards Java is the  Difference betwixt EnumMap as well as HashMap inwards Java

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

0 Response to "Difference Betwixt Enummap In Addition To Hashmap Inwards Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel