Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What's the additional value of 'EnumMap' ?

Reply
Thread Tools

What's the additional value of 'EnumMap' ?

 
 
peter@yahoo.not
Guest
Posts: n/a
 
      09-12-2004
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' ?
 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-12-2004
writes:

> 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
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      09-12-2004
Lasse Reichstein Nielsen wrote:
> 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?
 
Reply With Quote
 
noname
Guest
Posts: n/a
 
      09-12-2004
Michael Borgwardt wrote:
> Lasse Reichstein Nielsen wrote:
>
>> 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?


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.
 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      09-12-2004
noname wrote:
>>> 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?

>
> 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.
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      09-12-2004
noname <> writes:

> 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
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Additional DC Hollywood0728 MCSE 5 09-05-2008 02:28 PM
EnumMap and jsp? Rusty Wright Java 1 10-17-2007 02:19 PM
Passing enum and EnumMap to a method ~kurt Java 4 05-15-2007 11:52 PM
additional RAM davo Computer Support 14 08-20-2006 02:25 AM
Creating an EnumMap in a generic Mick Java 5 06-25-2006 01:46 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57