【发布时间】:2013-02-15 00:26:30
【问题描述】:
我想计算两个哈希映射的键的并集。我写了以下代码(下面是MWE),但是我
得到 UnsupportedOperationException。这样做有什么好处?
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class AddAll {
public static void main(String args[]){
Map<String, Integer> first = new HashMap<String, Integer>();
Map<String, Integer> second = new HashMap<String, Integer>();
first.put("First", 1);
second.put("Second", 2);
Set<String> one = first.keySet();
Set<String> two = second.keySet();
Set<String> union = one;
union.addAll(two);
System.out.println(union);
}
}
【问题讨论】: