How to create a Multimap from a Map>?

前端 未结 6 1279
难免孤独
难免孤独 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:28

    Assuming you have

    Map> map = ...;
    Multimap 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> entry : map.entrySet()) {
      multimap.putAll(entry.getKey(), entry.getValue());
    }
    

提交回复
热议问题