Issue
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?
Solution
Yes EnumSet is modifiable. If you want to create an immutable EnumSet then go for immutableEnumSet.
- Returns an immutable set instance containing the given enum elements. Internally, the returned set will be backed by an
EnumSet. - The iteration order of the returned set follows the enum's iteration order, not the order in which the elements appear in the given collection.
Parameters: elements - the elements, all of the same enum type, that the set should contain
Returns: an immutable set containing those elements, minus duplicates
Answered By - Subash J
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.