Skip to content

Instantly share code, notes, and snippets.

@Adverte
Created January 9, 2019 14:12
Show Gist options
  • Save Adverte/32f74d3f5edbc5f2852566d97ef11bd3 to your computer and use it in GitHub Desktop.
Save Adverte/32f74d3f5edbc5f2852566d97ef11bd3 to your computer and use it in GitHub Desktop.
Stream example
list.stream()
.collect(Collectors.collectingAndThen(
Collectors.groupingBy(A::getId, Collector.of(
() -> new A<>(null, 0d, null), (acc, a) -> {
acc.setId(a.getId());
acc.setQuant(Double.sum(acc.getQuant(), a.getQuant()));
acc.setType(a.getType());
}, (a1, a2) -> {
a1.setQuant(Double.sum(a1.getQuant(), a2.getQuant()));
return a1;
}, a -> new B<>(a.getId(), new NewClass(a.getQuant(), a.getType()) )
)), Map::values
))
// Set .forEach(System.out::println);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment