Java 8 Stream API with examples

What is the stream?

Java Stream API

Streams are wrappers around a data source, which allows us to operate with that data source and making bulk processing convenient and fast. A stream represents a sequence of objects from a source, which supports aggregate operations.

The following are the characteristics of a Stream:

The sequence of elements: A stream provides a set of elements of a specific type in a sequential manner. A stream gets/computes elements on demand. It never stores the elements.

Source: Stream takes Collections, Arrays, or I/O resources as an input source.

Aggregate operations: Stream supports aggregate operations like filter, map, limit, reduce, find, match, and so on.

Pipelining: Most of the stream operations return stream itself so that their result can be pipelined. These operations are called intermediate operations and their function is to take input, process them, and return output to the target. collect() method is a terminal operation that is normally present at the end of the pipelining operation to mark the end of the stream.

Automatic iterations: Stream operations do the iterations internally over the source elements provided, in contrast to Collections where explicit iteration is required.

Let’s see a few ways to obtain a stream:

    1. Using existing Array
int [] arr = new int[] {1,2,3,4,5,6,7,8,9,10};

    1. Using existing Collection
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);

System.out.println("List Stream\n");;

Set<Integer> set = new HashSet<>(list);

System.out.println("\nSet Stream\n");;

Map<Integer, String> map = new HashMap<>();

map.put(1, "one");
map.put(2, "two");
map.put(3, "three");

System.out.println("\nMap Stream\n");

    1. Stream from individual objects using Stream.of()
    1. Using Stream.builder()
Stream.Builder<String> builder = Stream.builder();

Stream<String> stream =;


Stream Operations: Now let’s try some common usages and operations we can perform on and with the help of stream API.


forEach() is the simplest and most common operation, it loops over the stream elements, calling the supplied function on each element. You can refer to the above code block to see the uses of foreach(). This method is so common that is has been added directly in Iterable, Map, etc. Observe the following code snippet.

import java.util.Arrays;
import java.util.List;

public class StreamDemo {

	public static void main(String[] args) {
		List<String> cities = Arrays.asList("Delhi","Mumbai","New York","London");
		System.out.println("===== Conventional way of looping the list =====");
		for(int i =0 ; i< cities.size() ; i++){
		System.out.println("===== Using for each loop =====");
		for(String str : cities){
		System.out.println("===== Using stream and method reference =====");;
		System.out.println("===== Using stream and lambda ====="); ->{

Once you execute this the output will as below:

===== Conventional way of looping the list =====
New York
===== Using for each loop =====
New York
===== Using stream and method reference =====
New York
===== Using stream and lambda =====
New York

Similar to forEach() we can also use a stream to perform many more tasks. Like filtering the data, getting count, finding distinct elements, replacing items with mapping new values, etc. Observe the below code snippet, where we are filtering the cities starting with ‘D’ from the city list.

List<String> cities = Arrays.asList("Delhi","Mumbai","New York","London","Dublin");
List<String> startingWithD = -> c.startsWith("D")).collect(Collectors.toList());
System.out.println("City name start with d "+startingWithD);

// Output:
City name start with d [Delhi, Dublin]

Recommended Read:

Java 8 method reference with example
Java 8 Predicates
Java 8 forEach method in Iterable Interface
Java 8 forEach method in Iterable Interface
Java 8 Default and Static method in Interface
Java 8 – Comparison with Lambda


Java 8 Stream

Leave a Comment