向map中追加元素


 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}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM