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
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())));
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()))));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With