Skip to main content

Posts

Showing posts with the label Java 8 sorting in streams

Java 8 - Aggregate operations on Streams

This post is in continuation with my earlier posts on Streams . In this post we will discuss about aggregate operations on Streams. Aggregate operations on Streams You can perform intermediate and terminal operations on Streams. Intermediate operations result in a new stream and are lazily evaluated and will start when terminal operation is called. persons.stream().filter(p -> p.getGender() == Gender.MALE).forEach(System.out::println); In the snippet above, filter() doesn't start filtering immediately but create a new stream. It will only start when terminal operation is called and in above case when forEach() . Intermediate operations There are many intermediate operations that you can perform on Streams. Some of them are filter() , distinct() , sorted() , limit() , parallel() , sequential , map() , flatMap . filter() operation This takes Predicate functional interface as argument and the output stream of this operation will have only those elements which pass th