Difference Betwixt Enummap Together With Hashmap Inward Java
HashMap vs EnumMap inwards Java
What is the divergence betwixt EnumMap in addition to HashMap inwards Java is the latest Java collection interview question which has been asked to a span of my friends? This is i of the tricky Java questions, specially if you lot are non really much familiar amongst EnumMap inwards Java, which is non uncommon, given you lot tin flame utilization it amongst exactly Enum keys. The primary difference betwixt EnumMap in addition to HashMap is that EnumMap is a specialized Map implementation solely for Enum every bit key. Using Enum every bit primal allows doing closed to implementation marking optimization for high surgical operation which is mostly non possible amongst other objects every bit key. We lead hold seen a lot of interview questions on HashMap inwards our article How HashMap plant inwards Java but what nosotros missed at that spot is this enquiry which is latterly asked to closed to of my friends. Unlike HashMap, EnumMap is non applicable for every instance but it's best suited when you lot lead hold Enum every bit key. We lead hold already covered basics of EnumMap in addition to closed to EnumMap instance inwards my terminal article What is EnumMap inwards Java in addition to In this post, nosotros volition focus on primal differences betwixt HashMap in addition to EnumMap inwards Java.
EnumMap vs HashMap
Before looking differences betwixt EnumMap in addition to HashMap, few words well-nigh What is mutual betwixt them. Both of them implements Map interface so they tin flame endure used inwards all methods which lead hold Map in addition to information tin flame endure 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 surgical operation for mutual methods e.g. get() or put(). Now let's encounter few differences betwixt EnumMap vs HashMap :
1) As said earlier, starting fourth dimension in addition to initiatory off divergence betwixt EnumMap in addition to HashMap is that EnumMap is optimized for enum keys spell HashMap is a full general travel Map implementation similar to Hashtable. you lot tin flame non utilization whatsoever type other than Enum every bit primal inwards EnumMap but you lot tin flame utilization both Enum in addition to whatsoever other Object every bit primal inwards HashMap.
2) Another divergence betwixt EnumMap in addition to HashMap is performance. every bit discussed inwards the previous point, due to specialized optimization done for Enum keys, EnumMap is probable to perform amend than HashMap when using enum every bit primal object.
3) One to a greater extent than affair which tin flame endure considered every bit the divergence betwixt HashMap in addition to EnumMap is the probability of Collision. Since Enum is internally maintained every bit array in addition to they are stored inwards their natural gild 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 band hashCode method on keys, at that spot is no lead chances of collision.
These were closed to notable difference betwixt EnumMap in addition to HashMap inwards Java. In brusk EnumMap is best suited for enum keys, for which it has optimized in addition to performed amend than HashMap inwards Java. Use EnumMap whenever you lot tin flame utilization enum every bit keys.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt Hashtable in addition to ConcurrentHashMap inwards Java

Komentar
Posting Komentar