
The hack with two-dimensional array and Stream API is pretty ugly if you ask me, and gets uglier if you need to create a Map whose keys and values are not the same type (like Map in the question).Īs for future of Guava in general, with regards to Java 8, Louis Wasserman said this back in 2014, and in 2016 it was announced that Guava 21 will require and properly support Java 8. If you don't want Guava dependency, consider a plain old init method.

MAP JAVA 8 UPDATE
Update (2015): As for Java 8, well, I would still use the Guava approach because it is way cleaner than anything else. (If you are new to it, there are some excellent learning resources behind that link.) If you aren't using this library in your Java project yet, I strongly recommend trying it out! Guava has quickly become one of the most popular and useful free 3rd party libs for Java, as fellow SO users agree. (A subset of) Guava used to be called Google Collections. To learn more about the benefits of Guava's immutable collection utilities, see Immutable Collections Explained in Guava User Guide.

Instead, try ImmutableMap.builder() along these lines: static final Map MY_MAP = ImmutableMap.builder()

If you want the map to have more than 5 entries, you can no longer use ImmutableMap.of(). I like the Guava way of initialising a static, immutable map: static final Map MY_MAP = ImmutableMap.of(Īs you can see, it's very concise (because of the convenient factory methods in ImmutableMap).
