How to create a Multimap from a Map>?

前端 未结 6 1284
难免孤独
难免孤独 2020-12-03 01:02

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<         


        
6条回答
  •  醉话见心
    2020-12-03 01:07

    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 multimap = ArrayListMultimap.create();
    Map> 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.

提交回复
热议问题