Image for post
Image for post
Photo by Shahadat Rahman on Unsplash

Introduction

Java stream API is not a really new API. This API has been introduced by Oracle since Java version 8. As specified inside the JDK documentation, Java stream is A sequence of elements supporting sequential and parallel aggregate operations (source: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html).

public class Student {
private int age;

private String name;

public int getAge ( ) {
return age;
}

public void setAge ( int age ) {
this.age = age;
}

public String getName ( ) {
return name;
}

public void setName ( String name ) {
this.name = name;
}
}
public class ClassRoom {
private List< Student > students = new ArrayList<> ( );

public List< Student > getStudents ( ) {
return students;
}

public void add ( Student student ) {
this.students.add ( student );
}
}
ClassRoom classRoom = new ClassRoom ( );

for ( int i = 0 ; i < 100 ; i++ ) {
int age = ( int ) ( Math.random ( ) * 20.0 + 1.0 );
String name = "Student" + ( i + 1 );

Student student = new Student ( );
student.setAge ( age );
student.setName ( name );

classRoom.add ( student );
}

ForEach

The forEach function performs an action for each element of this stream. This is a new way of doing repetition of action to be performed on each of the item in the stream. Mostly, we can use the forEach function instead of employing the traditional for construct.

classRoom.getStudents ( ).stream ( ).forEach ( student ->
System.out.println ( "Name: " + student.getName ( ) + ", Age: " + student.getAge ( ) ) );
  • From the list of students that we have, we call the stream function, to get the stream of data from the collection.
  • From the stream data that we have, we call the forEach function.
Name: Student1, Age: 9
Name: Student2, Age: 14
Name: Student3, Age: 5
Name: Student4, Age: 10
Name: Student5, Age: 16
Name: Student6, Age: 6
Name: Student7, Age: 17
Name: Student8, Age: 16
...

Map

The map function returns a stream consisting of the results of applying the given function to the elements of this stream. This definition might sound abstract for those who are new to this concept. In short, the map function accepts the data and do transformation of the data based on the provided function. This transformed data then returned in a form of another stream.

List studentNames = classRoom.getStudents ( ).stream ( )
.map ( student -> student.getName ( ) ).collect ( Collectors.toList ( ) );
studentNames.stream ( ).forEach ( System.out::println );
Student1
Student2
Student3
Student4
Student5
Student6
Student7
Student8
...

Sum

Now, let’s say we want to get the sum of the age of all the students, we can utilise the sum function. This function though is not provided by the map directly. It is though provided by the LongStream, DoubleStream, and IntStream which can be produced by calling mapToLong, mapToDouble, and mapToInt respectively.

int sum = classRoom.getStudents ( ).stream ( )
.mapToInt ( student -> student.getAge ( ) ).sum ( );
System.out.println ( "Sum of age: " + sum );
Sum of age: 1117

Filter

Filter is one of the most powerful functions provided by the stream API. Filter returns a stream consisting of the elements of this stream that match the given predicate.

classRoom.getStudents ( ).stream ( )
.filter ( student -> student.getAge ( ) >= 15 && student.getAge ( ) <= 17 )
.forEach ( student ->
System.out.println ( "Name: " + student.getName ( ) + ", Age: " + student.getAge ( ) ) );
Name: Student5, Age: 16
Name: Student7, Age: 17
Name: Student8, Age: 16
Name: Student18, Age: 15
Name: Student31, Age: 15
Name: Student35, Age: 17
Name: Student39, Age: 17
Name: Student48, Age: 17
Name: Student52, Age: 16
Name: Student53, Age: 17
Name: Student61, Age: 15
Name: Student64, Age: 15
Name: Student73, Age: 16
Name: Student88, Age: 16
Name: Student97, Age: 15

Conclusion

As we can see, Java stream API provides useful functions we can use to make it easier to deal with collections. On all the example above, we can avoid the use of traditional for..loop.. construct and use the more functional approach to solve the problem and achieve what we want.

Written by

I am a software engineer focusing on Java programming language and Public Key Infrastructure (PKI). Loves Linux and open-source technology.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store