向map中追加元素

时间:2021-01-01 10:45:13
 1 public class Demo01 {
 2     
 3     public static void main(String[] args) {
 4         
 5         String mapKey = "a";
 6         Map<String, Map<String, Integer>> maps = new ConcurrentHashMap<String,Map<String, Integer>>();
 7         Map<String, Integer> map = new HashMap<String,Integer>();
 8         map.put("aaaa", 1);
 9         map.put("bbbb", 2);
10         map.put("cccc", 3);
11         
12         maps.put("a", map);
13         
14         if(maps.containsKey(mapKey)){
15             Map<String, Integer> tmpMap = maps.get(mapKey);            
16             tmpMap.put("dddd", 4);    
17             maps.put(mapKey, tmpMap);
18         }
19         
20         //遍历maps
21         Iterator<String> it = maps.keySet().iterator();
22         while(it.hasNext()){
23             String mapsKey = it.next();        
24             Map<String, Integer> mapValue = maps.get(mapsKey);
25             System.out.println(mapValue);
26         }
27     }
28 
29 }
30 
31 输出结果:
32 {bbbb=2, dddd=4, aaaa=1, cccc=3}