Skip to content
Justice Tech School

Streams

Streams process collections with map, filter and collect.

Example
import java.util.*;
import java.util.stream.*;
public class Main {
  public static void main(String[] args) {
    List<Integer> n = List.of(1, 2, 3, 4, 5);
    List<Integer> sq = n.stream().filter(x -> x % 2 == 1).map(x -> x * x).collect(Collectors.toList());
    System.out.println(sq + " " + n.stream().mapToInt(x -> x).sum());
  }
}

Result

[1, 9, 25] 15

Exercise

Keep items.

stream().(x -> x > 1)

Quiz Test

What does map do in a stream?

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