Is there any way to modify an EnumSet object? It seems that (for example) EnumSet.add is inherited from AbstractSet and that throws an UnsupportedOperationException.
Yet it looks as if EnumSet is modifiable; otherwise why bother having a clone() method, and why would Guava have Sets.immutableEnumSet()? Is there a method of EnumSet that's escaped my mind?
Yes EnumSet is modifiable. If you want to create an immutable EnumSet then go for immutableEnumSet.
EnumSet.Parameters: elements - the elements, all of the same enum type, that the set should contain
Returns: an immutable set containing those elements, minus duplicates
EnumSet is just an abstract class. It's implementation overrides add method which makes it mutable and hence the below is possible
EnumSet<MyEnum> enumSet = EnumSet.of(MyEnum.A);
enumSet.add(MyEnum.B);
private static enum MyEnum {
   A,B,C;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With