2
如何使用addAll()將映射條目添加到來自同步映射的同步/非同步映射 - Java?如何從同步映射將映射條目添加到同步/非同步映射java
Map<String,String> h = Collections.synchronizedMap(new HashMap<String,String>());
Map<String,String> g = Collections.synchronizedMap(new HashMap<String,String>());
// or even better - Map<String,String> = new HashMap<String,String>();
h.put("kkk","eee");
Set<Map.Entry<String,String>> s = h.entrySet();
Set<Map.Entry<String,String>> ss = g.entrySet();
ss.addAll(s); //causes unsupportedoperationexception for both the synchronized version
// of map g and the unsynchronized, even if this operation is placed
// in a synchronized block (synchronized(h){}, and synchronized(g){})