Need of EnumSet / EnumMap over generified Set / Map

EnumSet/EnumMap can be created by specifying the defined enum to produce set/map instance as shown in below sample code.

So far I read, difference between EnumSet/EnumMap with that of Set/Map is that we cannot add objects other than the specified Enum in the EnumSet/EnumMap.

If this is the case, then just the generified Set/Map itself will be enough, isn't it?

Please find the EnumSet/EnumMap and their respective generified Set/Map as follows,

enum Value {

VALUE_1, VALUE_2, VALUE_3

};

public class Sample {

public static void main(String args[]) {

EnumSet enumSet = EnumSet.of(Value.VALUE_1);

Set enumGenerifiedSet = new HashSet();

enumGenerifiedSet.add(Value.VALUE_1);

EnumMap enumMap...

Read More »

By: StackOverFlow - 5 days ago

Related Posts