Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Immutable version of EnumSet

Tags:

I can't find an Immutable version of EnumSet. Two questions:

Can I use Enums in a normal Guava ImmutableSet?

If I can, what are some benefits/drawbacks of using an ImmutableSet instead of the EnumSet?

like image 598
Carlos Bribiescas Avatar asked Feb 09 '15 15:02

Carlos Bribiescas


People also ask

Is EnumSet immutable?

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 .

What is EnumMap and EnumSet?

EnumMap is a specialized implementation of the Map interface for the enumeration types. EnumSet is a specialized implementation of the Set interface for the enumeration types. EnumMap is internally represented as an array. EnumSet is internally represented as a BitVector.

What is the use of EnumSet?

The EnumSet is one of the specialized implementations of the Set interface for use with the enumeration type. A few important features of EnumSet are as follows: It extends AbstractSet class and implements Set Interface in Java. EnumSet class is a member of the Java Collections Framework & is not synchronized.

How do you create an empty EnumSet in Java?

Use EnumSet. noneOf(Class) to create an empty EnumSet.


1 Answers

Are you looking for Sets.immutableEnumSet (Guava) perhaps?

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.

like image 187
Jon Skeet Avatar answered Oct 04 '22 07:10

Jon Skeet