Skip to content
Justice Tech School

Streams and Collectors

Streams process collections declaratively: filter, map, sorted, distinct, limit, reduce and collect. Collectors group, join and count.

Example
import java.util.*;
import java.util.stream.*;
List<String> names = List.of("Ada", "Tunde", "Zainab", "Ali");
System.out.println(names.stream().filter(n -> n.length() > 3).map(String::toUpperCase).collect(Collectors.toList()));
System.out.println(names.stream().collect(Collectors.groupingBy(String::length, Collectors.counting())));
System.out.println(IntStream.rangeClosed(1, 5).sum() + " " + names.stream().collect(Collectors.joining("|")));

Result

[TUNDE, ZAINAB]
{3=2, 5=1, 6=1}
15 Ada|Tunde|Zainab|Ali

Exercise

Keep matching items.

stream.(x -> x > 1)

Quiz Test

What is a terminal operation?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.