Is there a corresponding immutable enumMap in guava?
I was just wanted to provide an example now that ImmutableEnumMap
is in Guava 14.0, because it's a package-private class, so you can't do ImmutableEnumMap.of()
. You have to do Maps.immutableEnumMap()
instead.
private final ImmutableMap<MyEnum, String> myEnumMap = Maps.immutableEnumMap(ImmutableMap.of(
MyEnum.A, "A",
MyEnum.B, "B",
MyEnum.C, "C"
));
Not sure if there's a more natural syntax.
Guava contributor here.
Guava doesn't currently have an ImmutableEnumMap
variant, but if it did, it would probably just be a wrapper around an EnumMap
. (That said, slightly better immutable implementations are possible.)
EnumMap
will perform better than the basic ImmutableMap
, in any event; it's difficult or impossible to beat.
(I'll file an issue to investigate adding an ImmutableMap
variant for enum key types, though.)
Update: Guava 14 adds Maps.immutableEnumMap()
.