What is the best way to combine two Maps into a single Guava MultiMap in Java?
For example:
Then the resulting combined multimap would contain
This is my current solution:
Multimap<T, K> combineMaps(Map<T, K> map1, Map<T, K> map2) {
Multimap<T, K> multimap = new MultiMap();
for (final Map.Entry<T, K> entry : map1.entrySet()) {
multimap.put(entry.getKey(), entry.getValue());
}
for (final Map.Entry<T, K> entry : map2.entrySet()) {
multimap.put(entry.getKey(), entry.getValue());
}
return multimap;
}
...What sort of multimaps are these? Are they from Guava, or some other library?
In Guava, you could do
multimap.putAll(Multimaps.forMap(map1));
multimap.putAll(Multimaps.forMap(map2));