Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create and Invert MultiMap w/ Java 8 Streams

How can I convert a Set<Result> into a Map<Item, Set<String>> or SetMultimap<Item, String> using Java 8 streams or Multimaps, where Result is:

class Result {
    String name;
    Set<Item> items;
}

For example, I start with:

result1:
    name: name1
    items:
        - item1
        - item2
result2:
    name: name2
    items:
        - item2
        - item3

and end with:

item1:
    - name1
item2:
    - name1
    - name2
item3:
    - name2
like image 888
mathematician Avatar asked Apr 06 '17 04:04

mathematician


2 Answers

two important methods in code snippet below are Stream.flatMap & Collectors.mapping:

import java.util.Map.Entry;
import java.util.AbstractMap.SimpleEntry;
import static java.util.stream.Collectors.*;

results.stream()
    //map Stream<Result> to Stream<Entry<Item>,String>
   .flatMap(it -> it.items.stream().map(item -> new SimpleEntry<>(item, it.name)))
   //group Result.name by Item 
   .collect(groupingBy(Entry::getKey, mapping(Entry::getValue, toSet())));
like image 155
holi-java Avatar answered Nov 17 '22 03:11

holi-java


Once there is Collectors.flatMapping in jdk-9, it could look a little different. Also your Item instances need to be comparable in some way, or you can specify that in groupBy (let's say by a field 'itemName'):

Map<Item, Set<String>> map = results.stream()
            .collect(Collectors.flatMapping(
                    (Result result) -> result.getItems()
                            .stream()
                            .map((Item item) -> new AbstractMap.SimpleEntry<>(item, result.getName())),
                    Collectors.groupingBy(Entry::getKey, () -> new TreeMap<>(Comparator.comparing(Item::getItemName)),
                            Collectors.mapping(Entry::getValue, Collectors.toSet()))));
like image 33
Eugene Avatar answered Nov 17 '22 02:11

Eugene