How to create a Multimap from a Map>?

前端 未结 6 1255
难免孤独
难免孤独 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<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.

    0 讨论(0)
  • 2020-12-03 01:09

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

    0 讨论(0)
  • 2020-12-03 01:16

    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();
    }
    
    0 讨论(0)
  • 2020-12-03 01:17
    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));
    
    0 讨论(0)
  • 2020-12-03 01:24

    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());
    }
    
    0 讨论(0)
  • 2020-12-03 01:28

    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());
    }
    
    0 讨论(0)
提交回复
热议问题