合并包含集合的映射会抛出 UnsupportedOperationException
Merging Maps containing Sets throws UnsupportedOperationException
代码如下:
private static Map<String, Set<String>> merge(Map<String, Set<String>> m1, Map<String, Set<String>> m2) {
Map<String, Set<String>> mx = new HashMap<String, Set<String>>();
for (Entry<String, Set<String>> entry : m1.entrySet()) {
Set<String> otherMapValue = m2.get(entry.getKey());
if (otherMapValue == null) {
mx.entrySet().add(entry);
} else {
Set<String> merged = new HashSet<String>();
merged.addAll(entry.getValue());
merged.addAll(otherMapValue);
mx.put(entry.getKey(), merged);
}
}
return mx;
}
这将引发以下错误:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractCollection.add(Unknown Source)
at algorithms.NetworkBuilder.merge(NetworkBuilder.java:86)
at algorithms.NetworkBuilder.build(NetworkBuilder.java:38)
at algorithms.Main.main(Main.java:35)
我只找到了不包含集合的地图的解决方案,它们对我不起作用,因为如果两个地图中都出现一个键,我还需要合并集合。
我想要做的是创建一个新地图,其中包含两个地图中的一个或两个的每个键都映射到它在原始两个地图中映射到的列表的并集。
Returns a Set view of the mappings contained in this map. [...] The set supports
element removal, which removes the corresponding mapping from the map,
via the Iterator.remove, Set.remove, removeAll, retainAll and clear
operations. It does not support the add or addAll operations.
尝试 mx.put(entry.getKey(), entry.getValue())
而不是 mx.entrySet().add(entry)
。
如果允许您使用第三方库,请考虑使用 Guava 的 Multimap
。
Comparison [of Multimap
s] to a map of collections
Multimap
s are commonly used in places where a Map<K, Collection<V>>
would otherwise have appeared.
Multimap<String, String> m1 = ...
Multimap<String, String> m2 = ...
m1.putAll(m2); // merged!
你的代码问题在行
mx.entrySet().add(entry);
您使用的集合仅支持移除操作:
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html#entrySet()
您可能想将该行更改为
mx.put(entry.getKey(), entry.getValue());
此外,您的方法不考虑 m2
中但不在 m1
中的键。
您可能还想遍历 m2.entrySet()
。
代码如下:
private static Map<String, Set<String>> merge(Map<String, Set<String>> m1, Map<String, Set<String>> m2) {
Map<String, Set<String>> mx = new HashMap<String, Set<String>>();
for (Entry<String, Set<String>> entry : m1.entrySet()) {
Set<String> otherMapValue = m2.get(entry.getKey());
if (otherMapValue == null) {
mx.entrySet().add(entry);
} else {
Set<String> merged = new HashSet<String>();
merged.addAll(entry.getValue());
merged.addAll(otherMapValue);
mx.put(entry.getKey(), merged);
}
}
return mx;
}
这将引发以下错误:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractCollection.add(Unknown Source)
at algorithms.NetworkBuilder.merge(NetworkBuilder.java:86)
at algorithms.NetworkBuilder.build(NetworkBuilder.java:38)
at algorithms.Main.main(Main.java:35)
我只找到了不包含集合的地图的解决方案,它们对我不起作用,因为如果两个地图中都出现一个键,我还需要合并集合。
我想要做的是创建一个新地图,其中包含两个地图中的一个或两个的每个键都映射到它在原始两个地图中映射到的列表的并集。
Returns a Set view of the mappings contained in this map. [...] The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.
尝试 mx.put(entry.getKey(), entry.getValue())
而不是 mx.entrySet().add(entry)
。
如果允许您使用第三方库,请考虑使用 Guava 的 Multimap
。
Comparison [of
Multimap
s] to a map of collections
Multimap
s are commonly used in places where aMap<K, Collection<V>>
would otherwise have appeared.
Multimap<String, String> m1 = ...
Multimap<String, String> m2 = ...
m1.putAll(m2); // merged!
你的代码问题在行
mx.entrySet().add(entry);
您使用的集合仅支持移除操作: http://docs.oracle.com/javase/7/docs/api/java/util/Map.html#entrySet()
您可能想将该行更改为
mx.put(entry.getKey(), entry.getValue());
此外,您的方法不考虑 m2
中但不在 m1
中的键。
您可能还想遍历 m2.entrySet()
。