Common Pitfalls
Avoid frequent mistakes with Streams: reusing streams, hidden side effects, and parallel + mutable state.
Common Pitfalls is a free Java Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Streams are powerful, but there are pitfalls: reusing a Stream, hidden side effects, and mixing parallel with mutable state.
One-shot streams
A Stream is one-shot: after a terminal operation (forEach, collect, reduce) it cannot be used again.
Code: reuse error
After a terminal operation the Stream is consumed; attempting to reuse it throws IllegalStateException.
import java.util.*;import java.util.stream.*;public class Main {
public static void main(String[] args) {
List<Integer> nums = Arrays.asList(1, 2, 3);
Stream<Integer> s = nums.stream();
s.forEach(System.out::println); // terminal
try {
s.forEach(System.out::println); // reuse -> IllegalStateException
} catch (IllegalStateException e) {
System.out.println("Oops: " + e.getMessage());
}
}
}
Side effects
Avoid side effects inside map/filter (like mutating external lists). Prefer pure functions and collect() at the end.
Code: side effects vs collect
Mutating state in the pipeline is fragile. The collect() version is clearer and safer.
import java.util.*;import java.util.stream.*;public class Main {
public static void main(String[] args) {
List<Integer> nums = Arrays.asList(1,2,3);
// Pitfall: mutating external list from map/forEach
List<Integer> out = new ArrayList<>();
nums.stream().map(n -> { out.add(n*n); return n*n; }).forEach(x -> {});
System.out.println("With side effects: " + out);
// Better: use collect to build the result
List<Integer> squares = nums.stream().map(n -> n*n).collect(Collectors.toList());
System.out.println("With collect: " + squares);
}
}
Code: parallel + mutable state
Parallel streams + mutable containers can cause race conditions. Prefer collect() which handles concurrency.
import java.util.*;public class Main {
public static void main(String[] args) {
List<Integer> nums = java.util.Arrays.asList(1,2,3,4,5);
// Pitfall: adding to a normal ArrayList from parallel pipeline (unsafe)
List<Integer> unsafe = new ArrayList<>();
nums.parallelStream().forEach(n -> unsafe.add(n)); // may be unsafe
System.out.println("Parallel add to ArrayList (unsafe demo): " + unsafe);
// Safer: use built-in collectors
List<Integer> safe = nums.parallelStream().map(n -> n).collect(java.util.stream.Collectors.toList());
System.out.println("Collected safely: " + safe);
}
}
Pitfalls check
Quick check: Which is a common Stream pitfall for beginners?
Recap
Recap: 1) Streams are one-shot. 2) Avoid side effects in pipelines. 3) Do not mix parallel streams with unsafe mutable state.
Frequently asked questions
Is the “Common Pitfalls” lesson free?
Yes — the full text of “Common Pitfalls” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Common Pitfalls”?
Avoid frequent mistakes with Streams: reusing streams, hidden side effects, and parallel + mutable state. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Common Pitfalls” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Ordering & Short-circuiting
- Parallel Streams Basics
- Common Pitfalls