When there is Collectors.flatMapping in jdk-9, it may look a little different. Also, your Item instances must be comparable in some way, or you can specify this in groupBy (say, by the "itemName" field):
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()))));
source share