How to create a Multimap<K,V> from a Map<K, Collection<V>>?

JavaGuavaMultimap

Java Problem Overview


I didn't find such a multimap construction... When I want to do this, I iterate over the map, and populate the multimap. Is there an other way?

final Map<String, Collection<String>> map = ImmutableMap.<String, Collection<String>>of(
            "1", Arrays.asList("a", "b", "c", "c"));
System.out.println(Multimaps.forMap(map));

final Multimap<String, String> expected = ArrayListMultimap.create();
for (Map.Entry<String, Collection<String>> entry : map.entrySet()) {
    expected.putAll(entry.getKey(), entry.getValue());
}
System.out.println(expected);

The first result is {1=[[a, b, c, c]]} but I expect {1=[a, b, c, c]}

Java Solutions


Solution 1 - Java

Assuming you have

Map<String, Collection<String>> map = ...;
Multimap<String, String> multimap = ArrayListMultimap.create();

Then I believe this is the best you can do

for (String key : map.keySet()) {
  multimap.putAll(key, map.get(key));
}

or the more optimal, but harder to read

for (Entry<String, Collection<String>> entry : map.entrySet()) {
  multimap.putAll(entry.getKey(), entry.getValue());
}

Solution 2 - Java

This question is a little old, but I thought I'd give an updated answer. With Java 8 you could do something along the lines of

ListMultimap<String, String> multimap = ArrayListMultimap.create();
Map<String, Collection<String>> map = ImmutableMap.of(
                           "1", Arrays.asList("a", "b", "c", "c"));
map.forEach(multimap::putAll);
System.out.println(multimap);

This should give you {1=[a, b, c, c]}, as desired.

Solution 3 - Java

Here is a useful generic version that I wrote for my StuffGuavaIsMissing class.

/**
 * Creates a Guava multimap using the input map.
 */
public static <K, V> Multimap<K, V> createMultiMap(Map<K, ? extends Iterable<V>> input) {
  Multimap<K, V> multimap = ArrayListMultimap.create();
  for (Map.Entry<K, ? extends Iterable<V>> entry : input.entrySet()) {
    multimap.putAll(entry.getKey(), entry.getValue());
  }
  return multimap;
}

And an immutable version:

/**
 * Creates an Immutable Guava multimap using the input map.
 */
public static <K, V> ImmutableMultimap<K, V> createImmutableMultiMap(Map<K, ? extends Iterable<V>> input) {
  ImmutableMultimap.Builder<K, V> builder = ImmutableMultimap.builder();
  for (Map.Entry<K, ? extends Iterable<V>> entry : input.entrySet()) {
    builder.putAll(entry.getKey(), entry.getValue());
  }
  return builder.build();
}

Solution 4 - Java

UPDATE: For what you're asking, I think you're going to need to fall back to Multimap.putAll.

Solution 5 - Java

You can use the com.google.common.collect.Multimaps.flatteningToMultimap as your stream collector.

Given map:

Map<Integer, Collection<String>> map = Map.of(
   1, List.of("a", "b", "c"),
   2, List.of("d", "e", "f"));

You can do the following:

Multimap<Integer, String> multimap = map.entrySet().stream()
    .collect(flatteningToMultimap(Map.Entry::getKey, it -> it.getValue().stream(), HashMultimap::create));

Solution 6 - Java

Following code without Google's Guava library. It is used for double value as key and sorted order

Map<Double,List<Object>> multiMap = new TreeMap<Double,List<Object>>();

for( int i= 0;i<15;i++)
{
	List<Object> myClassList = multiMap.get((double)i);
	if(myClassList == null)
	{
		myClassList = new ArrayList<Object>();
		multiMap.put((double) i,myClassList);
	}
	myClassList.add("Value "+ i);
}

List<Object> myClassList = multiMap.get((double)0);
if(myClassList == null)
{
	myClassList = new ArrayList<Object>();
	multiMap.put( (double) 0,myClassList);
}
myClassList.add("Value Duplicate");
for (Map.Entry entry : multiMap.entrySet()) 
{
  System.out.println("Key = " + entry.getKey() + ", Value = " +entry.getValue());
}

Solution 7 - Java

LinkedListMultimap<String, String> mm = map.entrySet()
    .stream()
    .collect(
        () -> LinkedListMultimap.create(map.size()),
        (m, e) -> m.putAll(e.getKey(), e.getValue()),
        (m1, m2) -> m1.putAll(m2));

Solution 8 - Java

There is another method one can use: Multimaps#newMultimap

If you are using a collection extending List or Set, the docs recommend using the special methods Multimaps#newListMultimap and Multimaps#newSetMultimap.

Multimaps.newListMultimap(myMap, ArrayList::new);
Multimaps.newSetMultimap(myMap, HashSet::new);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsly7_7View Question on Stackoverflow
Solution 1 - JavaKevin BourrillionView Answer on Stackoverflow
Solution 2 - JavaDillon Ryan ReddingView Answer on Stackoverflow
Solution 3 - JavaDaniel AlexiucView Answer on Stackoverflow
Solution 4 - JavaHank GayView Answer on Stackoverflow
Solution 5 - JavaRadosław PanuszewskiView Answer on Stackoverflow
Solution 6 - JavaSomnath KadamView Answer on Stackoverflow
Solution 7 - JavaHéctor Muñoz BerzosaView Answer on Stackoverflow
Solution 8 - JavanathanfrankeView Answer on Stackoverflow