31

I recently learnt about the benefits of EnumMap in Java and would like to replace the existing ImmutableMap<OccupancyType, BigDecimal> to EnumMap. However, I'd also like the immutable property offered by ImmutableMap.

  • Is there a variant, ImmutableEnumMap available in guava ?
  • In terms of storage which one (EnumMap vs ImmutableMap) performs better ?
  • I couldn't find a comparison of the two. I'd appreciate if someone can point me to a link or give some insights on the efficiency of the two data structures ?
brainydexter
  • 19,826
  • 28
  • 77
  • 115

3 Answers3

39

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().

Pezo
  • 1,458
  • 9
  • 15
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Thanks. Would you happen to have a comparison of the two handy ? – brainydexter Jun 28 '12 at 12:52
  • 1
    I mean, there's not exactly a well-defined metric by which to compare them, but I can tell you that `ImmutableMap` is only slightly better than `HashMap`, so `EnumMap` should beat both by around the same factor. – Louis Wasserman Jun 28 '12 at 13:04
  • 13
    Update: `ImmutableEnumMap` [has been added.](http://code.google.com/p/guava-libraries/source/detail?r=835a7397fabb05a764a8008f4151c97f821a29ef) – Louis Wasserman Oct 09 '12 at 00:22
  • Yup, `ImmutableEnumMap` is now in Guava 14.0. =) – The Alchemist Mar 08 '13 at 13:48
  • @LouisWasserman As an aside, is there any plans for `EnumMultimap`? – durron597 May 06 '13 at 18:13
  • @louis-wasserman: Is there a guava-collections Maven artefact that includes this, or is it only in the guava artefact? It doesn't seem to be included in guava-collections R03. – Michael Scheper Dec 03 '13 at 04:40
  • 1
    Apologies. I see now that the ImmutableEnumMap class that I found code for is not public, and that I should use Maps.immutableEnumMap(Map). – Michael Scheper Dec 03 '13 at 04:55
  • Note: `immutableEnumMap()` does not return an `EnumMap`. So if you want this because you want to pass an immutable object to something expecting an `EnumMap` (in the same way that you might pass an `ImmutableMap` to something expecting a `Map`), it won't work. – bacar Oct 02 '14 at 17:14
14

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.

The Alchemist
  • 3,397
  • 21
  • 22
  • 4
    Within that call to `Maps.immutableEnumMap` you can use a `ImmutableMap.builder().put(MyEnum.A, "A").build()` to add more than 5 values to the map. – drvdijk Jun 17 '14 at 13:59
3

As the guava ImmutableEnumMap is still marked beta as of version 14, I would suggest using a unmodifiable view of a enum map and then throwing away the original reference to the enum map to ensure that it is immutable.

Example (in a constructor):

Map entries = new EnumMap <SomeEnum, T>(SomeEnum.class);
... // (fill in entries)
this.entries = Collections.unmodifiableMap(entries);
jmh
  • 8,856
  • 1
  • 18
  • 26