What's the additional value of 'EnumMap' ?

P

peter

What's the additional value of 'EnumMap' ?

I'm asking this, because it seems to me that the same
result can be accomplished by using a plain 'HashMap' (?).
See these two examples :

******

Example 01 (with 'EnumMap') :

public enum TrafficLightColor { red, orange, green };

public void getInstruction()
{
EnumMap<TrafficLightColor, String> instructions =
new EnumMap<TrafficLightColor, String>(TrafficLightColor.class);
instructions.put(TrafficLightColor.red, "Stop");
instructions.put(TrafficLightColor.orange, "Slow down or accelerate");
instructions.put(TrafficLightColor.green, "Drive");

TrafficLightColor k = TrafficLightColor.red;
System.out.printf("The instruction for color %s is : '%s'.", k, instructions.get(k));
}

******

Example 02 (with 'HashMap') :

public enum TrafficLightColor { red, orange, green };

public void getInstruction()
{
HashMap<TrafficLightColor, String> instructions =
new HashMap<TrafficLightColor, String>();
instructions.put(TrafficLightColor.red, "Stop");
instructions.put(TrafficLightColor.orange, "Slow down or accelerate");
instructions.put(TrafficLightColor.green, "Drive");

TrafficLightColor k = TrafficLightColor.red;
System.out.printf("The instruction for color %s is : '%s'.", k, instructions.get(k));
}

******

The outcome is the same, so what's the big difference ?
Why (or when) would one prefer to use 'EnumMap'
in favor to 'HashMap' ?
 
L

Lasse Reichstein Nielsen

Why (or when) would one prefer to use 'EnumMap'
in favor to 'HashMap' ?

Efficiency. EnumMap is a specialized implementation of Map, where
HashMap is very general. Specializing allows using non-general
optimizations. An EnumMap is optimized to hold only enumeration
values, and it doesn't need to, e.g., call hashcode() on the
values.

As enum values can be mapped to small integers, an EnumMap might be
implemented using an array. (Hmm, that's easy to check, the source
code for EnumMap is available. And yes, it uses an array :).

/L
 
M

Michael Borgwardt

Lasse said:
As enum values can be mapped to small integers, an EnumMap might be
implemented using an array. (Hmm, that's easy to check, the source
code for EnumMap is available. And yes, it uses an array :).

You mean, exactly like HashMap does?
 
N

noname

Michael said:
You mean, exactly like HashMap does?

He probably means the algorithm is optimized for never having a hash
collision, so it never has to check the list of values at each bucket.
Or maybe not...I haven't looked at the source code.
 
M

Michael Borgwardt

noname said:
He probably means the algorithm is optimized for never having a hash
collision, so it never has to check the list of values at each bucket.
Or maybe not...I haven't looked at the source code.

It probably does work like that, but in any case it's certainly not the
use of an array to which the keys are mapped that makes EnumMap more
efficient, but merely the *way* the mapping is done.
 
L

Lasse Reichstein Nielsen

noname said:
He probably means the algorithm is optimized for never having a hash
collision, so it never has to check the list of values at each
bucket.

Indeed. It is a simple array, with only (at most) one value at each
index.
I haven't checked HashMap, but a typical hash map implementation would
have (linked) lists as entries in the array.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top