Streams against loops

A stream is a good tool with four sharp edges, and every one of them has appeared in the course already: a stream can be read once, a step may be skipped when the answer does not need it, a lambda cannot throw a checked exception, and a lambda that changes something outside itself is a bug waiting for a parallel run. This lesson puts a program on each edge, then the case where the loop is the better code after all.

A stream used twice

A stream is a cursor over its source and not a container, so the second terminal operation on the same object has nothing left to read. The message says "stream has already been operated upon or closed":

import java.util.List; import java.util.stream.Stream; public class Reuse { public static void main(String[] args) { Stream<String> words = List.of("a", "b").stream(); System.out.println(words.count()); try { System.out.println(words.count()); } catch (IllegalStateException e) { System.out.println("second use: " + e.getClass().getSimpleName()); } List<String> list = List.of("a", "b"); System.out.println(list.stream().count() + " " + list.stream().count()); } }

javac -Xlint:all Reuse.java
java Reuse

2
second use: IllegalStateException
2 2

The last line is the fix: the collection can be asked for a stream as often as the code likes, and each call is a new cursor. A method that takes a Stream as a parameter is promising to read it once, which is why the JDK's methods take collections and streams only where the stream is the whole point.

The step that never runs

Lazy means that a step runs only if the terminal operation needs its result, and count does not need the elements, only how many there are. The map with the printing body is skipped, and the same map under collect runs twice:

import java.util.List; public class Counts { public static void main(String[] args) { long n = List.of("a", "b").stream() .map(w -> { System.out.println("mapping " + w); return w; }) .count(); System.out.println("count " + n + " and no mapping line"); long m = List.of("a", "b").stream() .map(w -> { System.out.println("mapping " + w); return w; }) .collect(java.util.stream.Collectors.toList()) .size(); System.out.println("size " + m); } }

javac -Xlint:all Counts.java
java Counts

count 2 and no mapping line
mapping a
mapping b
size 2

The consequence is a rule: a step whose only purpose is a side effect is a step the JDK may skip, so a body that prints, logs or counts must not be a map or a filter in a pipeline whose terminal operation does not need it. The tools for a side effect are forEach and, when the effect is real work, a loop.

A checked exception in a lambda

The functional interfaces of java.util.function declare no checked exception, so a lambda that calls a method which throws one does not compile, even inside a method that declares the exception:

import java.util.List; public class CheckedCompile { public static void main(String[] args) throws Exception { List<String> lines = List.of("1", "2"); long count = lines.stream().map(line -> { return read(line); }).count(); System.out.println(count); } static String read(String line) throws java.io.IOException { return line; } }

javac -Xlint:all CheckedCompile.java

CheckedCompile.java:7: error: unreported exception IOException; must be caught or declared to be thrown
return read(line);
^
1 error

The throws Exception on main does not help, because the lambda is an implementation of Function.apply, which declares no exception. Three ways out exist: catch inside the lambda and rethrow as an unchecked exception, use a method that does the same, or write the loop, which is inside main and may declare the exception.

The first is the usual one, and it keeps the cause so the stack trace still names the original failure: throw new UncheckedIOException(e) for an I/O failure, which the exceptions part of the course reaches in lesson 099. A stream over files is therefore a stream of already-read values, not of reads.

Side effects

A lambda that changes a variable outside itself works in a sequential pipeline and fails in a parallel one, and it cannot change a local variable at all: the capture rule of lesson 082 forbids it. So the only side effects a pipeline may have are on objects the elements point at, such as an array slot or a field of a record's holder.

The measurement of lesson 091 is the proof: ten thousand adds into one ArrayList from a parallel forEach produced lists of 3312, 8359 and 6511 elements, with no exception. A pipeline is a description of a computation over values, and the moment it also changes something, it stops being one.

When the loop wins

Run-length encoding reads a list of words and writes each run as the word and its length: a2 b3 c1. The loop keeps the previous word and the length of the current run, which is state that moves from one element to the next. A pipeline can compute the same counts with groupingBy, and it cannot produce the run order without a stateful lambda:

import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Both { public static void main(String[] args) { List<String> words = List.of("a", "a", "b", "b", "b", "c"); StringBuilder loop = new StringBuilder(); String previous = null; int run = 0; for (String word : words) { if (word.equals(previous)) { run++; } else { if (previous != null) { loop.append(previous).append(run).append(" "); } previous = word; run = 1; } } loop.append(previous).append(run); System.out.println(loop); Map<String, Long> counts = words.stream() .collect(Collectors.groupingBy(w -> w, Collectors.counting())); System.out.println(counts); System.out.println(counts.size() + " groups"); } }

javac -Xlint:all Both.java
java Both

a2 b3 c1
{a=2, b=3, c=1}
3 groups

Both programs give the same counts, and only the loop gives the runs in the order of the list, because the order is the state it carries. The pipeline is shorter and says "group and count", which is the question; the loop says "compare with the previous element", which is a different question that happens to produce the same numbers for this input.

The rule that follows is the one this part of the course has used: write the pipeline when the work is a filter, a map, a sort, a group, a count or a sum, and write the loop when the body has to remember something between elements, has to break early with a result, or throws a checked exception. A pipeline that needs a comment to say what it carries is a loop that was written the hard way.

Exercises

  1. Store a stream in a variable, call findFirst on it and then count, and print the exception.
  2. Add a step to Counts that prints only for the collect version and explain why.
  3. Wrap a checked exception in an unchecked one inside a lambda and print its cause.
  4. Rewrite the run-length loop with a Map and explain which output it does not produce.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What happens when a stream is used a second time?

    Answer

    The terminal operation throws IllegalStateException

    The stream is a cursor, and the second operation finds it consumed.

  2. Why did the map with the printing body not run under count?

    Answer

    count needs only the number of elements, so the step is skipped

    Laziness lets the implementation skip work the answer does not need.

  3. Why can a lambda not throw a checked exception?

    Answer

    The functional interface it implements declares no checked exception

    The method of the interface has no throws clause, so the lambda may not add one.

  4. What does this program print? import java.util.List; import java.util.stream.Stream; public class Q1 { public static void main(String[] args) { Stream<String> s = List.of("a").stream(); System.out.println(s.count()); try { s.count(); } catch (IllegalStateException e) { System.out.println("twice"); } } }

    Answer

    1 / twice

    The first count works and the second finds the stream consumed.

  5. What does this program print? import java.util.List; import java.util.stream.Collectors; public class Q2 { public static void main(String[] args) { long count = List.of("a", "b", "c").stream() .peek(w -> System.out.println("peek " + w)) .count(); System.out.println("count " + count); } }

    Answer

    count 3

    count does not read the elements, so the peek body is never needed.

  6. What is the first line of the compiler output for this program? import java.util.List; public class Q3 { public static void main(String[] args) throws Exception { List.of("a").stream().map(s -> read(s)).count(); } static String read(String s) throws java.io.IOException { return s; } }

    Answer

    error: unreported exception IOException; must be caught or declared to be thrown

    The lambda implements a method without a throws clause, so the checked exception is not allowed.

  7. What does this program print? import java.util.List; import java.util.stream.Collectors; public class Q4 { public static void main(String[] args) { List<Integer> numbers = List.of(1, 2, 3); int sum = 0; List<Integer> doubled = numbers.stream() .map(n -> n * 2) .collect(Collectors.toList()); for (int n : doubled) { sum += n; } System.out.println(sum); } }

    Answer

    12

    The doubled values are 2, 4 and 6, and their sum is twelve.

  8. What does this program print? import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class Q5 { public static void main(String[] args) { List<String> words = List.of("a", "a", "b"); Map<String, Long> counts = words.stream() .collect(Collectors.groupingBy(w -> w, Collectors.counting())); System.out.println(counts.get("a")); System.out.println(counts.size()); } }

    Answer

    2 / 2

    The word a is counted twice and there are two distinct words.

  9. Which job belongs in a loop rather than a pipeline?

    Answer

    One that remembers the previous element between steps

    A pipeline step sees one element, so state that crosses elements is a loop or a collector.

  10. Why is a side effect in a lambda a risk even in a sequential stream?

    Answer

    The step may be skipped or reordered, so the effect may not happen

    Laziness and the optimisation of terminal operations both can drop a step.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev

Banner image