Search code examples
javacollectionssetimmutability

Immutable version of EnumSet


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?


Solution

  • 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.