What is stream api in java 8 with examples

Nikhil Soman Sahu
3 min readApr 15, 2023

--

Java Stream API is a powerful tool introduced in Java 8 to process and manipulate collections of data in a concise and functional way. It allows developers to write cleaner, more readable, and efficient code for working with data.

what is stream api in java 8 with examples

What is a Stream?

A Stream is a sequence of elements that can be processed in parallel or sequentially. It is not a data structure and does not store any data. Instead, it provides a pipeline of operations that can be performed on the data. The pipeline consists of a source, zero or more intermediate operations, and a terminal operation.

Source: The source of a stream can be a collection, an array, or an I/O channel.

Intermediate operations: These operations transform the data in the stream. They can be used in a chain to perform complex operations. Some examples of intermediate operations are map(), filter(), and distinct().

Terminal operation: A terminal operation is the final step in a stream pipeline. It produces a result or a side effect. Examples of terminal operations are forEach(), collect(), and reduce().

Benefits of Java Stream API

Concise and readable code: Java Stream API allows developers to write code that is more concise and readable. This is because the stream operations can be chained together to form a pipeline, making the code more declarative and less verbose.

Parallel processing: Java Stream API supports parallel processing, which can significantly improve the performance of operations on large data sets. This means that operations can be distributed across multiple threads, allowing them to be executed simultaneously.

Lazy evaluation: Java Stream API uses lazy evaluation, which means that operations are only executed when required. This improves performance and reduces memory usage.

Strong typing: Java Stream API is strongly typed, which means that errors can be caught at compile time rather than at runtime. This improves code reliability and reduces the likelihood of errors occurring.

Examples of Java Stream API

  1. Filtering elements in a list:
List<String> names = Arrays.asList("John", "Mary", "Peter", "Adam");

List<String> filteredNames = names.stream()
.filter(name -> name.startsWith("J"))
.collect(Collectors.toList());

In this example, the stream filters all the elements in the list that start with the letter “J”.

2. Mapping elements in a list:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

List<Integer> squaredNumbers = numbers.stream()
.map(number -> number * number)
.collect(Collectors.toList());

In this example, the stream maps each element in the list to its square value.

3. Reducing elements in a list:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
.reduce(0, (a, b) -> a + b);

In this example, the stream reduces all the elements in the list to their sum value.

Java 8 Streams Cheat Sheet

Java 8 Streams Cheat Sheet

--

--

Nikhil Soman Sahu
Nikhil Soman Sahu

Written by Nikhil Soman Sahu

Sr Software Developer | Spring Boot | Flutter | Dart | Java

No responses yet