Skip to main content


Showing posts with the label Java 8 reduction

Java 8 - Streams in Action

In this post, we will cover following topics. What are Streams? What is a pipeline? Key points to remember for Streams. How to create Streams? What are Streams? Java 8 introduced new package which contains classes to perform SQL-like operations on elements. Stream is a sequence of elements on which you can perform aggregate operations (reduction, filtering, mapping, average, min, max etc.). It is not a data structure that stores elements like collection but carries values often lazily computed from source through pipeline . What is a pipeline? A pipeline is sequence of aggregate (reduction and terminal) operations on the source. It has following components. A source: Collections, Generator Function, array, I/O channel etc. zero or more intermediate operations: filter, map, sequential, sorted, distinct, limit, flatMap, parallel etc. Intermediate operations returns/produces stream. a termination operation: forEach, reduction, noneMatch, allMatch, c

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. -> 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