java中Map和List初始化的两种方法
1 //初始化List2 List<string> list = new ArrayList</string><string>();
3 list.add("string1");
4 list.add("string2");
5 //some other list.add() code......
6 list.add("stringN");
7
8 //初始化Map
9 Map<string object=""> map = new HashMap<string object="">();
10 map.put("key1", "value1");
11 map.put("key2", "value2");
12 //.... some other map.put() code
13 map.put("keyN", "valueN");
14 </string></string></string>
1 //初始化List2 List<string> list = new ArrayList<string>(){{
3 add("string1");
4 add("string2");
5 //some other add() code......
6 add("stringN");
7 }};
8
9 //初始化Map
10 Map<string string=""> map = new HashMap<string object="">(){{
11 put("key1", "value1");
12 put("key2", "value2");
13 //.... some other put() code
14 put("keyN", "valueN");
15 }};
ArrayList<string> list = new ArrayList(Arrays.asList("Tom", "Jerry", "Mike"));
List list = new ArrayList<string>(){{add("A");
add("B");
}}
以上是 java中Map和List初始化的两种方法 的全部内容, 来源链接: utcz.com/z/390086.html