Better Map Constructor

后端 未结 5 630
走了就别回头了
走了就别回头了 2021-01-02 10:13

Is there a more streamlined way to do the following?

Map map = new HashMap();
map.put(\"a\", \"apple\");
map.put(         


        
5条回答
  •  囚心锁ツ
    2021-01-02 10:36

    You could always use double brace initialization:

    Map map = new HashMap() {{
        put("foo", "bar");
        put("baz", "qux");
    }}
    

    But bear in mind this might not be efficient according to these answers.

提交回复
热议问题