Stream StormPart 1 of 6
Java Streams Explained Visually: filter, map, flatMap, sorted | Stream Storm Part 1
A stream sends each item through a few steps. filter keeps items that pass a test, map changes every item, flatMap unpacks lists inside a list, and sorted puts them in order. Nothing runs until you ask for a result.
The code from the video
java
List<Integer> numbers = List.of(3, 8, 1, 6, 5, 2);
List<Integer> result = numbers.stream()
.filter(n -> n % 2 == 0) // keep the even numbers
.map(n -> n * 10) // multiply each by ten
.toList();
System.out.println(result); // [80, 60, 20]flatMap unpacks lists inside a list, and sorted puts the result in order:
java
List<List<Integer>> nested = List.of(List.of(3, 8), List.of(1), List.of(6, 5, 2));
nested.stream()
.flatMap(List::stream)
.sorted()
.toList(); // [1, 2, 3, 5, 6, 8]
Next in the series
Java reduce() and groupingBy() Explained Visually | Stream Storm Part 2
Read the write-up
- Stream Storm3 min read
Java Streams for beginners: filter, map, flatMap, sorted
What a stream is, the four steps you'll use every day, and the one surprise that trips everyone up.
Related videos
- Short
Stream StormThis toMap() Call Crashes Your Java Code (and the Fix) | Stream Storm Part 3
· Part 3 of 6