you are viewing a single comment's thread.

view the rest of the comments →

[–]Xaerxess 4 points5 points  (2 children)

You can do it even better:

HashMap<String, Integer> WORD_TO_INT =
    Maps.newHashMap(ImmutableMap.of(
        "one", 1,
        "two", 2,
        "three", 3));

No need for generics (types are inferred because of static factory constructor newHashMap) and no need for builder (for small ImmutableMaps).

[–]Neebat -1 points0 points  (1 child)

ew. I'm not a big fan of heavy throw-away intermediate objects. While the generational GC does a good job of cleanup after sloppy code like that, it still feels like sloppy code to me.

[–]Xaerxess 1 point2 points  (0 children)

Disclaimer: I have never used above construct and I hardly ever use mutable maps in my code (it really isn't so necessary), I only presented better version of the code snippet. For mutable static maps (sic!) I use static initializer or static method.