0

I am new to Enum. While I am learning Enum, I came across EnumSet and EnumMap. I understand that when dealing with map and set using EnumSet and EnumMap is much better than the hashing counterparts from this question.

Why an EnumSet or an EnumMap is likely to be more performant than their hashed counterparts?

My question is

In what scenarios do we need Map and Set for Enum in Java?

I am more interested in knowing practical scenarios.

Community
  • 1
  • 1
mvg
  • 1,574
  • 4
  • 37
  • 63
  • When you want named "flag"s, you need `EnumSet` (much better in readability than using `int`s and bit-magic for example). When you want to assign values or defaults/functions to enum values, you need `EnumMap`. – Gábor Bakos Dec 31 '14 at 14:06
  • Your question is not crystal clear. Do you want to know about scenarios where EnumMap and EnumSet is preferred over HashMap and HashSet, or are you asking when should you use EnumMap and when EnumSet (lets say because you are confusing them)? – Pshemo Dec 31 '14 at 14:11
  • I just want to know, why or when we need a collection(Map and Set) for an enum. Sorry if my question is confusing. – mvg Dec 31 '14 at 14:13
  • Since EnumMap and EnumSet are better, you can use it whenever you can. – Peter Lawrey Dec 31 '14 at 14:16
  • My question is Why we need a Set or Map for Enum? – mvg Dec 31 '14 at 14:18
  • ...It's not clear what you mean. Do you understand when you want maps and sets in general? It's just those cases, except when the values are enums... – Louis Wasserman Dec 31 '14 at 14:18
  • I understand Maps and Sets in general. We can use it to collect data whose size and values are unknown mostly at the compile time. But enum values are predefined at the time of its creation. So why we need a collection for that? – mvg Dec 31 '14 at 14:22
  • You can intersect the `EnumSet`s, or add new values to it, so those are not constants. Similarly you can assign new values to existing keys to `EnumMap`s, or add, remove key/value pairs. – Gábor Bakos Dec 31 '14 at 14:27
  • 1
    @mvg: You know all the possible enum values at compile time, but you don't necessarily know which combination of them you'll want to use at runtime. Perhaps they're a subset of the possible enum values being read from a configuration file, or you're manipulating a subset of the days of the week. – Louis Wasserman Dec 31 '14 at 15:25

1 Answers1

1

Consider a scenario where you are defining flyweight for each shape object like:

public enum SHAPE {
    TRIANGLE, RECTANGLE, SQUARE, HEXAGON;
}

And let's assume object creation for all these shapes everytime is huge. Say it goes into DB and fetches some data say in constructor. What you do?

You save it in a map using enum key and value as one time object and then using enum, you say try and call something like:

public double calculateArea(SHAPE shape) {
    Figure figure = map.get(shape);//you dont create object but take it from a map
    return figure.calculate();
}

For EnumSet, consider you have enum with basic colors like:

enum COLORS {
    RED, BLUE, GREEN
}

Now if i want to create say YELLOW Color and wish to know the INGREDIENTS for the same, i could use EnumSet with two Colors like GREEN and RED. Similarly, i could store multiple set with variations as per my color requirements.

This is just for example purpose, but you could even add count to make it more complex to say how much of RED, BLUE, GREEN pixel variation you need.

SMA
  • 36,381
  • 8
  • 49
  • 73
  • I understand Enum is a collection of constants and you say that EnumSet is part of that collection - a subcollection. Correct? – mvg Dec 31 '14 at 14:24
  • Yes it extends [AbstractSet](http://docs.oracle.com/javase/7/docs/api/java/util/EnumSet.html) – SMA Dec 31 '14 at 14:26