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};
		
Stream.of(arr);

    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");
list.stream().forEach(System.out::println);

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

System.out.println("\nSet Stream\n");		
set.stream().forEach(System.out::println);

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

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

System.out.println("\nMap Stream\n");
map.entrySet().stream().forEach(System.out::println);

    1. Stream from individual objects using Stream.of()
Stream.of(1,2,3,4,5,6);
    1. Using Stream.builder()
Stream.Builder<String> builder = Stream.builder();
builder.accept("welcome");
builder.accept("to");
builder.accept("Java");
builder.accept("8");

Stream<String> stream = builder.build();

stream.forEach(System.out::println);

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

forEach:

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(cities.get(i));
		}
		
		System.out.println("===== Using for each loop =====");
		for(String str : cities){
			System.out.println(str);
		}
		
		System.out.println("===== Using stream and method reference =====");
		cities.stream().forEach(System.out::println);
		
		System.out.println("===== Using stream and lambda =====");
		cities.stream().forEach(city ->{
			System.out.println(city);
		});
	}
}

Once you execute this the output will as below:

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

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 = cities.stream().filter(c -> 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

References:

Java 8 Stream

Leave a Comment