Modern Java APIs · 9 min read · Beginner
Collections, Optional & Streams API
Transform Lists and Maps declaratively with Java Streams (filter, map, toList) and avoid NullPointerException with Optional.
What you'll learn
- Create immutable collections with List.of() and Map.of()
- Filter, transform, and aggregate data using the Streams API
- Handle missing values safely with Optional and .orElse()
The Java Collections Framework (List, Set, Map) paired with the Streams API lets you express data pipelines cleanly without mutable temporary lists.
Streams & Optional
list.stream().filter(...).map(...).toList()builds a clean transformation pipeline.Optional<T>signals explicitly that a lookup might return no value, encouraging safe handling with.orElse(...)instead of risking aNullPointerException.
Fill-in-the-Blank Exercise
Filter & Collect with Streams
Keep scores of 80 or higher and collect them into a List.
var passing = scores.()
.(s -> s >= 80)
.toList();Interactive Code Lab
JavaStreamsDemo.java
Expected OutputClick “Run Live” to compile with OpenJDK 21
Top scores: [88, 91, 95] Perfect found: 0
Check Your Understanding
Score: 0 / 1
1. What problem does Optional<T> help prevent in modern Java?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.