Skip to content

Instantly share code, notes, and snippets.

@marko-bekhta
Created October 25, 2017 19:26
public void newStreamsCollectorsSimpleCase() throws Exception {
// Java 8:
List<Student> students = getGroupStream()
.flatMap( gr -> gr.getStudents().stream() )
.filter( student -> student.getAverageMark() > 10L )
.collect( Collectors.toList() );
assertThat( students ).hasSize( 3 );
// new Java 9 Collectors#filtering collector:
students = getGroupStream()
.flatMap( gr -> gr.getStudents().stream() )
.collect( Collectors.filtering( student -> student.getAverageMark() > 10L, Collectors.toList() ) );
assertThat( students ).hasSize( 3 );
// Java 9 collecting right away without other operations:
students = getGroupStream()
.collect(
Collectors.flatMapping(
gr -> gr.getStudents().stream(),
Collectors.filtering(
student -> student.getAverageMark() > 10L,
Collectors.toList()
)
)
);
assertThat( students ).hasSize( 3 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment