java中Map和List初始化的两种方法

 1 //初始化List
 2 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 //初始化List
 2     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");
}}

 

posted @ 2016-04-05 16:56  鸭子船长  阅读(13627)  评论(0编辑  收藏  举报