向map中追加元素

时间:2023-03-09 00:46:06
向map中追加元素
 public class Demo01 {

     public static void main(String[] args) {

         String mapKey = "a";
Map<String, Map<String, Integer>> maps = new ConcurrentHashMap<String,Map<String, Integer>>();
Map<String, Integer> map = new HashMap<String,Integer>();
map.put("aaaa", 1);
map.put("bbbb", 2);
map.put("cccc", 3); maps.put("a", map); if(maps.containsKey(mapKey)){
Map<String, Integer> tmpMap = maps.get(mapKey);
tmpMap.put("dddd", 4);
maps.put(mapKey, tmpMap);
} //遍历maps
Iterator<String> it = maps.keySet().iterator();
while(it.hasNext()){
String mapsKey = it.next();
Map<String, Integer> mapValue = maps.get(mapsKey);
System.out.println(mapValue);
}
} } 输出结果:
{bbbb=2, dddd=4, aaaa=1, cccc=3}