builder for HashMap

前端 未结 15 2273
迷失自我
迷失自我 2020-11-30 00:17

Guava provides us with great factory methods for Java types, such as Maps.newHashMap().

But are there also builders for java Maps?

HashM         


        
15条回答
  •  盖世英雄少女心
    2020-11-30 00:41

    This is similar to the accepted answer, but a little cleaner, in my view:

    ImmutableMap.of("key1", val1, "key2", val2, "key3", val3);
    

    There are several variations of the above method, and they are great for making static, unchanging, immutable maps.

提交回复
热议问题