如何直接初始化HashMap(以字面方式)? [重复]

帅比萌擦擦* 提交于 2020-08-16 11:24:01

问题:

This question already has an answer here: 这个问题已经在这里有了答案:

Is there some way of initializing a Java HashMap like this?: 有没有办法像这样初始化Java HashMap?

Map<String,String> test = 
    new HashMap<String, String>{"test":"test","test":"test"};

What would be the correct syntax? 正确的语法是什么? I have not found anything regarding this. 我还没有发现任何有关此的信息。 Is this possible? 这可能吗? I am looking for the shortest/fastest way to put some "final/static" values in a map that never change and are known in advance when creating the Map. 我正在寻找在地图中放置一些“最终/静态”值的最短/最快方法,这些值永远不会改变,并且在创建地图时会事先知道。


解决方案:

参考一: https://stackoom.com/question/SXdT/如何直接初始化HashMap-以字面方式-重复
参考二: https://oldbug.net/q/SXdT/How-to-directly-initialize-a-HashMap-in-a-literal-way-duplicate
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!