Topic 10. Stream API and Optional

Stream API and Optional make Java data-processing code more predictable and readable. Streams model transformations as pipelines, while Optional makes “value may be absent” explicit. For beginners, this combination reduces null-related bugs and deeply nested imperative code.

Stream pipeline

1. Stream API Mental Model

A Stream is not a container. It is a computation pipeline over a source.

Core structure:

  1. source,
  2. intermediate operations (filter, map, flatMap, ...),
  3. terminal operation (toList, collect, count, findFirst, ...).

Example:

List<String> names = List.of("ann", "bob", "alex", "anna");

List<String> result = names.stream()
        .filter(n -> n.length() > 3)
        .map(String::toUpperCase)
        .sorted()
        .toList();

2. Stream Laziness

Intermediate operations are lazy. Until terminal operation is called, pipeline is not executed.

Stream<String> s = names.stream().filter(n -> n.startsWith("a"));
// not executed yet
long count = s.count(); // triggers execution

This enables optimization and avoids unnecessary processing.

3. Common Operations with Practical Examples

map

List<Integer> lengths = names.stream()
        .map(String::length)
        .toList();

filter

List<String> shortNames = names.stream()
        .filter(n -> n.length() <= 3)
        .toList();

flatMap

List<List<String>> tags = List.of(
        List.of("java", "backend"),
        List.of("backend", "spring")
);

List<String> uniqueTags = tags.stream()
        .flatMap(List::stream)
        .distinct()
        .sorted()
        .toList();

collect with grouping

Map<Integer, List<String>> byLength = names.stream()
        .collect(java.util.stream.Collectors.groupingBy(String::length));

reduce

List<Integer> prices = List.of(100, 250, 80, 70);
int total = prices.stream().reduce(0, Integer::sum);

4. Stream vs Loop: Practical Choice

Situation Better fit
Linear transformation Stream
Branch-heavy stateful logic Loop
Aggregation/grouping flow Stream
Step-by-step debugging Loop

Use what your team can read and maintain faster.

5. Optional: Explicit Absence Contract

Optional<T> communicates that value may be missing.

Optional<String> email = findEmailByUserId(10L);
String value = email.orElse("[email protected]");

Unlike hidden null, Optional forces explicit handling at call sites.

6. Core Optional Operations

  • orElse, orElseGet, orElseThrow
  • map, flatMap, filter
  • ifPresent, ifPresentOrElse

Null-safe chain example:

Optional<User> user = findUser(id);
String city = user
        .map(User::getAddress)
        .map(Address::getCity)
        .filter(c -> !c.isBlank())
        .orElse("Unknown");

7. orElse vs orElseGet

String a = opt.orElse(expensiveFallback());
String b = opt.orElseGet(() -> expensiveFallback());

orElse evaluates fallback eagerly. orElseGet evaluates lazily.

8. Frequent Beginner Mistakes

  1. Very long stream chains with dense business logic.
  2. Hidden side effects inside map/filter.
  3. Calling Optional.get() without a safety guarantee.
  4. Using parallelStream() without measurement.

9. Better vs Worse Examples

Worse:

String city = user.getAddress().getCity(); // NullPointerException risk

Better:

String city = Optional.ofNullable(user)
        .map(User::getAddress)
        .map(Address::getCity)
        .orElse("Unknown");

Worse:

list.stream().map(x -> { log(x); return transform(x); }).toList();

Prefer separating transformation from side-effect logic.

10. Key Takeaway

Streams are for declarative data pipelines. Optional is for explicit absence semantics. Together they improve clarity and safety when used with discipline: short readable chains, minimal side effects, and pragmatic stream-vs-loop choices.

Glossary
bytecode
Platform-independent compiled code that the JVM can execute.
class
A blueprint that defines fields and methods for objects in object-oriented programming.
object
A runtime instance of a class with its own state and behavior.
method
A named block of behavior that belongs to a class or object and can be called by code.
field
A variable stored inside a class or object that represents part of its state.
constructor
A special method-like block used to create and initialize a new object.
package
A namespace that groups related Java classes and helps organize code.
interface
A contract that defines methods a class can implement without prescribing all internal details.
generics
A language feature that lets code work with types as parameters while preserving type safety.
type erasure
The Java compiler process that removes most generic type details from runtime bytecode.
collection
A data structure that stores a group of elements and provides operations over them.
list
An ordered collection where elements have positions and duplicates are allowed.
set
A collection that stores unique elements and is commonly used for membership checks.
map
A key-value data structure where each key points to a value.
queue
A collection designed for processing elements in a defined order, often first-in-first-out.
lambda
A compact function-like expression used to pass behavior as data.
functional interface
An interface with one abstract method, suitable for lambdas and method references.
stream
A Java API for processing data sequences with operations such as map, filter, and reduce.
optional
A container object that represents a value that may or may not be present.
thread
An independent path of execution inside a running program.
synchronization
Coordination that protects shared data when multiple threads access it.
heap
The runtime memory area where Java objects are allocated.
stack
The runtime memory area that stores method calls, local variables, and call frames.
garbage collector
A JVM component that automatically frees memory occupied by objects no longer reachable.
memory leak
A situation where memory remains occupied because objects are still referenced but no longer useful.