问题:
This question already has an answer here: 这个问题已经在这里有了答案:
- How can I initialise a static Map? 如何初始化静态地图? 42 answers 42个答案
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
来源:oschina
链接:https://my.oschina.net/u/3797416/blog/4315684