【发布时间】:2012-03-23 22:34:55
【问题描述】:
如何使用 addAll() - 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){})
【问题讨论】:
标签: java collections map