What's the additional value of 'EnumMap' ?

Discussion in 'Java' started by peter, Sep 12, 2004.

  1. peter

    peter Guest

    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' ?
     
    peter, Sep 12, 2004
    #1
    1. Advertisements

  2. 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, Sep 12, 2004
    #2
    1. Advertisements

  3. You mean, exactly like HashMap does?
     
    Michael Borgwardt, Sep 12, 2004
    #3
  4. peter

    noname Guest

    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.
     
    noname, Sep 12, 2004
    #4
  5. 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.
     
    Michael Borgwardt, Sep 12, 2004
    #5
  6. 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, Sep 12, 2004
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.