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<Value> enumSet = EnumSet.of(Value.VALUE_1);
Set<Value> enumGenerifiedSet = new HashSet<Value>();
enumGenerifiedSet.add(Value.VALUE_1);
EnumMap<Value, Integer> enumMap = new EnumMap<Value, Integer>(Value.class);
enumMap.put(Value.VALUE_1, 1);
Map<Value, Integer> enumGenerifiedMap = new HashMap<Value, Integer>();
enumGenerifiedMap.put(Value.VALUE_1, 1);
}
}
So can you please tell me what is the need of having EnumSet/EnumMap
eventhough we can able to create the set/map that is generified to the defined Enum?
Thanks in advance.