Skip to content

Instantly share code, notes, and snippets.

@Jayasagar
Created December 17, 2017 15:21
Show Gist options
  • Save Jayasagar/941733ac396f71d3c9c67b55eb36725d to your computer and use it in GitHub Desktop.
Save Jayasagar/941733ac396f71d3c9c67b55eb36725d to your computer and use it in GitHub Desktop.
Map<String, Optional<Integer>> highestDeviceCostCustomerPaid = consumerList
.stream()
.flatMap(consumer -> consumer.getThings()
.stream()
.map(thing -> new AbstractMap.SimpleImmutableEntry<>(consumer.getName(), thing.getCost()))
)
.collect(Collectors.groupingBy(entry -> entry.getKey(),
Collectors.mapping(entry -> entry.getValue(), Collectors.maxBy(Comparator.comparingInt(value -> value)))));
System.out.println(highestDeviceCostCustomerPaid);
// Output: {Bob=Optional[300], Satti=Optional[300], Sri=Optional[200], Malli=Optional[120]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment