SyntaxPilot
Courses/Java/Lesson 37
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

Java · Lesson 37 of 51

Streams 1: map, filter, reduce

queued

These are the notes for Java episode 37. A wedding party asks Ravi what all their chai would cost in kulhads (clay cups, Rs 5 extra each). With loops that takes three loops, two throwaway lists and a running total. We invent the stream pipeline (a source, intermediate operations like filter and map, and a terminal operation like reduce), then learn how streams really run: lazily, one item at a time, and only once. We meet number streams (mapToInt, sum, average, IntStream), the most useful operations, where streams come from, and reduce with and without an identity. Every example was run with javac 21.

The problem: three loops for one question

import java.util.ArrayList;
import java.util.List;

record Order(String item, int price) {}

public class Main {
    public static void main(String[] args) {
        List<Order> orders = List.of(new Order("masala chai", 15),
                new Order("samosa", 12), new Order("ginger chai", 25),
                new Order("cold coffee", 60), new Order("masala chai", 15));

        List<Order> chais = new ArrayList<>();             // step 1: keep chai
        for (Order o : orders)
            if (o.item().contains("chai")) chais.add(o);
        List<Integer> inKulhad = new ArrayList<>();        // step 2: + Rs 5 cup
        for (Order o : chais) inKulhad.add(o.price() + 5);
        int total = 0;                                     // step 3: add up
        for (int p : inKulhad) total += p;
        System.out.println("Chai bill in kulhads: Rs " + total);
    }
}
Chai bill in kulhads: Rs 70

The question is one sentence: chai orders, plus five, added up. The code is fifteen lines of how: two lists that exist only to be thrown away and a total that keeps changing. Every new question means writing it all again.

The pipeline idea

Real-life example: a factory conveyor belt.

In a samosa factory In Java Meaning
The tray samosas come from Source orders.stream() puts the list's items on the belt
Stations: remove broken ones, add chutney Intermediate operations filter, map, sorted, ...: each returns a new stream
Packing at the end Terminal operation reduce, sum, count, toList, forEach: produces the result

A stream (java.util.stream.Stream<T>) is a sequence of elements flowing through a pipeline of operations. It does not store data (the list does) and it never changes its source.

import java.util.List;

record Order(String item, int price) {}

public class Main {
    public static void main(String[] args) {
        List<Order> orders = List.of(new Order("masala chai", 15),
                new Order("samosa", 12), new Order("ginger chai", 25),
                new Order("cold coffee", 60), new Order("masala chai", 15));

        int total = orders.stream()
                .filter(o -> o.item().contains("chai"))
                .map(o -> o.price() + 5)
                .reduce(0, (a, b) -> a + b);
        System.out.println("Chai bill in kulhads: Rs " + total);
    }
}
Chai bill in kulhads: Rs 70
  • filter(Predicate) keeps the elements where the test is true.
  • map(Function) turns each element into something else (here an Order into an Integer), so the belt now carries numbers.
  • reduce(identity, BinaryOperator) combines all elements into one value, starting from the identity 0: ((0 + 20) + 30) + 20 = 70.

Lazy: nothing runs until a terminal operation

import java.util.List;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        List<Integer> prices = List.of(15, 12, 25, 60);
        Stream<Integer> s = prices.stream()
                .filter(p -> {
                    System.out.println("checking " + p);
                    return p > 14;
                });
        System.out.println("Pipeline built. Anything checked?");
    }
}
Pipeline built. Anything checked?

No checking line at all. Intermediate operations only describe the work; nothing happens until a terminal operation asks for a result. This is called lazy evaluation.

Items flow one at a time, and limit stops early

import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> prices = List.of(15, 12, 25, 60, 40);
        List<Integer> firstTwo = prices.stream()
                .filter(p -> {
                    System.out.println("filter " + p);
                    return p > 14;
                })
                .map(p -> {
                    System.out.println("  map " + p);
                    return p * 2;
                })
                .limit(2)
                .toList();
        System.out.println(firstTwo);
    }
}
filter 15
  map 15
filter 12
filter 25
  map 25
[30, 50]

The stream does not filter all five and then map all five. Each element travels the whole pipeline before the next one starts (12 fails the filter and goes no further). Once limit(2) has two results, the stream stops: 60 and 40 are never looked at. Operations that can finish early (limit, findFirst, anyMatch, allMatch, noneMatch) are called short-circuiting.

A stream can be used only once

import java.util.List;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        Stream<String> menu = List.of("chai", "samosa", "vada pav").stream();
        System.out.println(menu.count() + " items");
        menu.forEach(System.out::println);
    }
}
3 items
Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
    ...
    at Main.main(Main.java:8)

A stream is like water from a tap: once it has flowed past, it's gone. After a terminal operation the stream is consumed. Don't keep streams in variables; call list.stream() again for each pipeline.

Number streams: IntStream, LongStream, DoubleStream

import java.util.List;

record Order(String item, int price) {}

public class Main {
    public static void main(String[] args) {
        List<Order> orders = List.of(new Order("chai", 15), new Order("samosa", 12));
        int total = orders.stream().map(Order::price).sum();
        System.out.println(total);
    }
}
Main.java:8: error: cannot find symbol
        int total = orders.stream().map(Order::price).sum();
                                                     ^
  symbol:   method sum()
  location: interface Stream<Integer>
1 error

A Stream<Integer> doesn't know how to add (you can't sum Strings or Orders), so sum() lives on the primitive streams IntStream, LongStream and DoubleStream. mapToInt converts:

import java.util.List;
import java.util.stream.IntStream;

record Order(String item, int price) {}

public class Main {
    public static void main(String[] args) {
        List<Order> orders = List.of(new Order("masala chai", 15),
                new Order("samosa", 12), new Order("ginger chai", 25),
                new Order("cold coffee", 60), new Order("masala chai", 15));

        int total = orders.stream().mapToInt(Order::price).sum();
        int dearest = orders.stream().mapToInt(Order::price).max().getAsInt();
        double avg = orders.stream().mapToInt(Order::price).average().orElse(0);
        System.out.println(total + " " + dearest + " " + avg);

        System.out.println(IntStream.rangeClosed(1, 5).map(n -> n * n).sum());
    }
}
127 60 25.4
55
Method (IntStream) Returns Note
sum() int 0 for an empty stream
count() long
max() / min() OptionalInt empty stream → empty box; open with getAsInt() or orElse(x)
average() OptionalDouble open with orElse(0)
IntStream.range(a, b) a … b-1 end excluded, like a for loop
IntStream.rangeClosed(a, b) a … b end included
boxed() Stream<Integer> back to a stream of objects

max and average return an Optional (a box that may be empty) because an empty stream has no maximum or average. Optional is covered in detail in episode 38.

More operations

import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> today = List.of("chai", "samosa", "chai", "vada pav",
                                     "poha", "chai", "samosa", "jalebi");
        List<String> menu = today.stream()
                .distinct()
                .sorted()
                .map(String::toUpperCase)
                .toList();
        System.out.println(menu);
        System.out.println(today.stream().skip(2).limit(3).toList());
        System.out.println(today.stream().filter(s -> s.equals("chai")).count());
        System.out.println(today.stream().anyMatch(s -> s.startsWith("v")));
        System.out.println(today.stream().allMatch(s -> s.length() > 3));
    }
}
[CHAI, JALEBI, POHA, SAMOSA, VADA PAV]
[chai, vada pav, poha]
3
true
true
Operation Kind Does
filter(p) intermediate keep elements where p is true
map(f) intermediate transform each element
mapToInt(f) intermediate transform to an IntStream
distinct() intermediate drop duplicates (uses equals)
sorted() / sorted(cmp) intermediate natural order / a Comparator
skip(n) / limit(n) intermediate drop the first n / keep at most n
peek(action) intermediate look at each element (for debugging)
forEach(action) terminal do something with each element
count() terminal how many elements (long)
reduce(id, op) terminal combine into one value
anyMatch / allMatch / noneMatch terminal true/false questions, short-circuiting
toList() (Java 16+) terminal an unmodifiable List
findFirst() terminal the first element, as an Optional

toList() returns an unmodifiable list:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> prices = new ArrayList<>(List.of(15, 12, 60));
        List<Integer> doubled = prices.stream().map(p -> p * 2).toList();
        System.out.println(prices + " " + doubled);
        doubled.add(1);
    }
}
[15, 12, 60] [30, 24, 120]
Exception in thread "main" java.lang.UnsupportedOperationException
    ...
    at Main.main(Main.java:9)

Need a list you can change? new ArrayList<>(stream.toList()), or collect(Collectors.toList()) (episode 38).

Where streams come from

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args) {
        Stream.of("chai", "samosa").forEach(System.out::println);

        int[] cups = {40, 55, 38};
        System.out.println(Arrays.stream(cups).sum() + " cups");

        IntStream.range(1, 4).forEach(day -> System.out.print("day" + day + " "));
        System.out.println();
        System.out.println("chai".chars().count() + " letters");
    }
}
chai
samosa
133 cups
day1 day2 day3 
4 letters
Source Gives
collection.stream() Stream<T> from a List, Set, ...
Stream.of(a, b, c) Stream<T> from values
Arrays.stream(array) Stream<T>, or IntStream for int[]
IntStream.range(1, 4) 1, 2, 3
"chai".chars() an IntStream of character codes
Files.lines(path) a Stream<String> of a file's lines (episode 35's files; close it with try-with-resources)

reduce, properly

import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> bills = List.of(30, 45, 12);
        int total = bills.stream().reduce((a, b) -> a + b);
        System.out.println(total);
    }
}
Main.java:6: error: incompatible types: Optional<Integer> cannot be converted to int
        int total = bills.stream().reduce((a, b) -> a + b);
                                         ^
1 error

Without a starting value, an empty stream has no answer, so reduce(op) returns Optional<Integer>. With an identity (the value that changes nothing: 0 for +, 1 for *, "" for joining strings) there is always an answer:

import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> bills = List.of(30, 45, 12);
        int total = bills.stream().reduce(0, (a, b) -> a + b);
        int biggest = bills.stream().reduce(0, Math::max);
        String line = List.of("chai", "samosa", "jalebi").stream()
                .reduce("", (acc, s) -> acc + s.charAt(0));
        System.out.println(total + " " + biggest + " " + line);

        List<Integer> none = List.of();
        System.out.println(none.stream().reduce(0, Integer::sum));
    }
}
87 45 csj
0

The empty list reduces to the identity 0, with no crash.

Loops vs streams

Loops Streams
describe how (indexes, temp lists, running totals) describe what (filter, map, sum)
change variables as they go never change the source
easy break/continue and complex control flow each step reads on its own
any number of passes over the data single use: one pass per stream

Both are fine Java. For keep, change, combine questions, streams say it best.

Common mistakes (and the real error messages)

Mistake What you see Why Fix
Pipeline with no terminal operation no error, nothing runs streams are lazy end with toList, sum, forEach, ...
Using one stream twice java.lang.IllegalStateException: stream has already been operated upon or closed a stream is single-use call list.stream() again
stream().map(Order::price).sum() error: cannot find symbol symbol: method sum() location: interface Stream<Integer> sum is only on number streams mapToInt(Order::price).sum()
int t = bills.stream().reduce((a, b) -> a + b); error: incompatible types: Optional<Integer> cannot be converted to int no identity: may be empty reduce(0, (a, b) -> a + b)
stream.toList().add(x) java.lang.UnsupportedOperationException toList() is unmodifiable new ArrayList<>(...)

Mini project: Ravi's end-of-day dashboard

import java.util.List;

record Order(String item, String type, int qty, int price) {}

public class Main {
    public static void main(String[] args) {
        List<Order> day = List.of(
                new Order("masala chai", "drink", 40, 15), new Order("samosa", "snack", 25, 12),
                new Order("ginger chai", "drink", 18, 25), new Order("cold coffee", "drink", 6, 60),
                new Order("vada pav", "snack", 30, 20), new Order("poha", "snack", 0, 30));

        int revenue = day.stream().mapToInt(o -> o.qty() * o.price()).sum();
        long unsold = day.stream().filter(o -> o.qty() == 0).count();
        int drinkCups = day.stream().filter(o -> o.type().equals("drink")).mapToInt(Order::qty).sum();
        double avgSnack = day.stream().filter(o -> o.type().equals("snack"))
                .mapToInt(o -> o.qty() * o.price()).average().orElse(0);
        List<String> top3 = day.stream()
                .sorted((a, b) -> Integer.compare(b.qty() * b.price(), a.qty() * a.price()))
                .limit(3)
                .map(Order::item)
                .toList();
        boolean bigSale = day.stream().anyMatch(o -> o.qty() * o.price() > 500);

        System.out.println("Revenue: Rs " + revenue);
        System.out.println("Items not sold: " + unsold);
        System.out.println("Cups of drinks: " + drinkCups);
        System.out.println("Average snack sales: Rs " + avgSnack);
        System.out.println("Top 3 by sales: " + top3);
        System.out.println("Any item over Rs 500? " + bigSale);
    }
}
Revenue: Rs 2310
Items not sold: 1
Cups of drinks: 64
Average snack sales: Rs 300.0
Top 3 by sales: [masala chai, vada pav, ginger chai]
Any item over Rs 500? true

sorted with a comparator that compares b with a gives the highest sales first; masala chai and vada pav tie at Rs 600 and keep their original order (sorting is stable).

Practice

  1. The sum of the squares of the even numbers from 1 to 100.
  2. Count the words longer than 5 letters in a sentence.
  3. The price of the most expensive order.
  4. Unique cities from a list, sorted A to Z.
Solution 1
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        int sum = IntStream.rangeClosed(1, 100).filter(n -> n % 2 == 0).map(n -> n * n).sum();
        System.out.println(sum);
    }
}
171700
Solution 2
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String text = "Ravi served masala chai and crispy samosas to hungry travellers";
        long n = Arrays.stream(text.split(" ")).filter(w -> w.length() > 5).count();
        System.out.println(n);
    }
}
6
Solution 3
import java.util.List;

record Order(String item, int price) {}

public class Main {
    public static void main(String[] args) {
        List<Order> orders = List.of(new Order("chai", 15), new Order("cold coffee", 60), new Order("samosa", 12));
        int max = orders.stream().mapToInt(Order::price).max().orElse(0);
        System.out.println(max);
    }
}
60
Solution 4
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> cities = List.of("Pune", "Delhi", "Pune", "Agra", "Delhi", "Chennai");
        System.out.println(cities.stream().distinct().sorted().toList());
    }
}
[Agra, Chennai, Delhi, Pune]

Quick quiz

System.out.println(
    Stream.of(3, 8, 5, 12, 7)
        .filter(n -> n % 2 == 1)
        .map(n -> n * 10)
        .reduce(0, Integer::sum));

A) 150 B) 350 C) 200 D) 15

Answer

A. filter keeps the odd numbers 3, 5, 7; map makes them 30, 50, 70; reduce adds them from 0.

150

Interview questions

  1. What is a stream? How is it different from a collection? A collection stores elements; a stream is a one-time pipeline of operations over elements from a source. Streams don't store data, don't modify the source, are lazy, and can be consumed only once.
  2. Intermediate vs terminal operations? Intermediate operations (filter, map, sorted) return a new stream and are lazy. A terminal operation (collect, reduce, forEach, count) triggers the processing and produces a result or side effect.
  3. What is lazy evaluation in streams? Nothing is processed until a terminal operation runs; then elements flow one at a time through the whole pipeline, which allows short-circuiting.
  4. map vs mapToInt? map returns Stream<R>; mapToInt returns an IntStream with sum, average, max and no boxing.
  5. Why can't a stream be reused? It is designed as a single pass over its source; after a terminal operation it is closed and throws IllegalStateException.
  6. What does reduce with and without an identity return? With an identity: a plain value (the identity for an empty stream). Without: an Optional.
  7. toList() vs collect(Collectors.toList())? toList() (Java 16) returns an unmodifiable list; Collectors.toList() currently returns a modifiable ArrayList (not guaranteed by the spec).
  8. Are streams always faster than loops? No. For small data, loops are often as fast or faster; streams win on readability. Parallel streams can help for large CPU-heavy work.

Glossary

Term Meaning Example
Stream A one-time pipeline of operations over a sequence of elements orders.stream()
Source Where a stream's elements come from a List, an array, Stream.of
Pipeline Source + intermediate operations + one terminal operation stream().filter().map().sum()
Intermediate operation Returns a new stream; lazy filter, map, sorted
Terminal operation Produces the result and runs the pipeline reduce, count, toList
filter Keeps elements that pass a Predicate filter(o -> o.price() > 10)
map Transforms each element with a Function map(o -> o.price() + 5)
reduce Combines all elements into one value reduce(0, Integer::sum)
Identity Starting value that changes nothing 0 for +, "" for joining
Lazy evaluation Work happens only when a terminal operation asks no output without a terminal op
Short-circuiting An operation that can stop early limit, anyMatch, findFirst
Consumed stream A stream that already ran its terminal operation IllegalStateException
IntStream A stream of int with sum, average, max mapToInt(Order::price)
mapToInt Converts to an IntStream mapToInt(String::length)
range / rangeClosed Number streams with end excluded / included IntStream.range(1, 4)
distinct Drops duplicates ["chai", "chai"] → ["chai"]
sorted Sorts the stream sorted()
skip / limit Drop the first n / keep at most n skip(2).limit(3)
anyMatch / allMatch / noneMatch Boolean questions about the elements anyMatch(s -> s.startsWith("v"))
count Number of elements, as a long filter(...).count()
toList Collects into an unmodifiable List (Java 16+) stream.toList()
Optional A box that may hold a value or be empty (episode 38) max(), reduce(op)
Unmodifiable list A list whose add/remove throw result of toList()
Stable sort Equal elements keep their order ties in sorted

Summary

  • A stream is a pipeline: a source, intermediate operations (filter, map, ...) and one terminal operation (reduce, sum, toList, ...).
  • filter keeps, map changes, reduce combines.
  • Streams are lazy: no terminal operation, no work. Items flow one at a time; limit and anyMatch stop early.
  • A stream can be used only once: get a new one from the list each time.
  • sum, average, max live on IntStream: use mapToInt.
  • reduce(identity, op) always has an answer; reduce(op) returns an Optional.
  • Next: totals per item, maps built from streams, and the Optional box: Streams 2: collectors, grouping, Optional (episode 38).