Photo by Jeffrey Wegrzyn on Unsplash

Stream API is data flows from one side to other side.

Stream API is introduced in Java 8 in order to bring functional programming into Java.

  • It is a data pipeline.
  • It use to process collection of objects or arrays.
  • It will not changed the original source.
  • It is efficient in coding.

In Stream API there are basically three major components as follows.

  • Source — Stream source can be any type of collection, array etc.
  • Intermediate Operations — In this, the incoming data stream will be converted according to the intermediate operation. Some intermediate operations are sort, map, filter.
  • Terminal Operations — This is the final operation of the stream. It will either return void, a single value or a collection. Some terminal operations are forEach, reduce, collect, min, sum, max, average.

Let’s do an example and figure it out. We need to print female student names whose gpa is greater than 3.6.


Let’s do this using Stream API.

Wow that is easy. Yes it is Let’s explore the operations in Stream API.

Operations in Stream API

First let’s start with how to make a stream. There are several method of doing this.

  • For Collections
  • For Arrays

Note: System.out::println is same as x -> System.out.println(x) .

Then we will move to some intermediate operations.

This is used to sort the input stream. It will either sort in alphabetical order or it can be sorted using attribute.

  • Primitive types
  • Sorted with specific attribute in an object — Following will order the students according to the age of the students.
  • Sorted with more than one attribute in an object — Following first sort by the age and then sort by the gpa.

In Filter operation input stream will filter using a condition and pass the filtered values forward.Stream.filter(x -> condition) .

  • Primitive type — This will filter the number greater than 3.
  • Filter with single attribute of an Object— This will filter students whose age is greater than 24.
  • Filter with multiple attributes of an Object — This will filter students whose age is greater than 24 and GPA is higher than 3.5.

Map Operation is used to update or alter the input stream and output altered stream. map(inputValue -> outputValue)

  • Primitive Type — This will convert the input numbers to its squares.
  • Map through the objects — Increment the age of the Student by one.

Above map function will get the input object one by one and increase the age by one and return the object to the next operation.

There are lots of intermediate operations and you can find when you need some are listed below.

  • findFirst()
  • skip()
  • peek()
  • flatMap()
  • distinct()

Then We will move to terminal operations.

This is one of a popular terminal operation which returns void. It is commonly used for printing.

This will return a Collection. We can get a List, Map etc as we need and we will dive into most needed use cases of this operation.

  • Get a list
  • Get a Map

For a map we need to provide to lambda functions one is for the key and other is for the value.

  • Group by an using a key

Above will group the Students using their age.

Reduce Operation will summarise the elements as we command as follows and reduce is having initial value, updated value holder and incoming value. Reduce function will return exactly one value.

reduce(initialvalue, (updatedValue, IncomingValue) -> operation);

Following is some example to add square of elements using reduce function.

There are lots of intermediate operations and you can find when you need some are listed below.

  • sum()
  • min()
  • max()
  • average()
  • summaryStatistics()

Hopefully this is helpful.

If you have found this helpful please hit that 👏 and share it on social media :).

Technical Writer | Tech Enthusiast | Open source contributor