Introduction Java 8 introduced a fundamentally different programming model. The features — lambdas, streams, Optional, CompletableFuture — interact with each other in ways that produce clean, readable code when used correctly and confusing, brittle code when used incorrectly. This article consolidates the most important production-ready guidance from across the series into a single reference, along with the migration checklist for moving a Java 7 codebase to Java 8. Streams Best Practices Do: Use streams for transformations and aggregations // Good: filter → transform → collect List<String> premiumNames = customers.
Continue reading »Optional
3 posts in this section
Optional: Eliminating NullPointerException the Right Way
The Problem Optional Solves NullPointerException is the most common runtime exception in Java, and it is almost always avoidable. Optional<T> is not a magic fix — used incorrectly it becomes a more verbose null check. Used correctly, it encodes the possibility of absence directly in the type system, so callers can never “forget” to handle the empty case. This article covers both how to use Optional and — critically — how not to.
Continue reading »Stream & Optional API Enhancements (Java 9–11)
Stream API — New Methods (Java 9) Java 9 added four new instance methods to Stream<T>. All are terminal-ish or intermediate operations that make common patterns expressible without workarounds. takeWhile(Predicate) Returns elements from the beginning of the stream as long as the predicate holds, then stops. The first element that fails the predicate (and all subsequent elements) are discarded. Stream.of(1, 2, 3, 4, 5, 1, 2) .takeWhile(n -> n < 4) .
Continue reading »