Java Programs›Java 8 Streams›Handle Missing Values Using Optional

Handle Missing Values Using Optional in Java

intermediate·  Java 8 Streams  ·  Streams

Problem

An Optional is a container that either holds a value or is empty, which makes "there might be no result" visible in the method's return type.

Search a list of usernames for the first one starting with a given letter, and handle both the found and not-found cases without null checks.

Input
[arjun, bella, chen, diya], prefix c and z
Output
Found: CHEN, Missing: nobody

Java Program

Java
import java.util.Arrays; import java.util.List; import java.util.Optional; public class OptionalWithStreams { public static void main(String[] args) { List<String> users = Arrays.asList("arjun", "bella", "chen", "diya"); Optional<String> found = users.stream().filter(u -> u.startsWith("c")).findFirst(); Optional<String> missing = users.stream().filter(u -> u.startsWith("z")).findFirst(); System.out.println("Found: " + found.map(String::toUpperCase).orElse("nobody")); System.out.println("Missing: " + missing.map(String::toUpperCase).orElse("nobody")); System.out.println("isPresent: " + found.isPresent() + ", " + missing.isPresent()); found.ifPresent(u -> System.out.println("Sending welcome email to " + u)); // runs only if present missing.ifPresent(u -> System.out.println("This line never prints")); } }

Output

Found: CHEN Missing: nobody isPresent: true, false Sending welcome email to chen

Core Logic

findFirst() returns an Optional, and its methods describe what to do in each case without ever touching null.

How It Works
  1. 1filter(...).findFirst() gives an Optional&lt;String&gt; that may be empty.
  2. 2map(String::toUpperCase) transforms the value only if it's present.
  3. 3orElse("nobody") supplies a fallback when it's empty.
  4. 4ifPresent(...) runs an action only when there's a value.
No username starts with z, so that line prints nobody and its ifPresent block never runs.
💡

Key Point: Calling get() on an empty Optional throws NoSuchElementException — prefer orElse, orElseThrow or ifPresent.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: The search stops at the first match, scanning at most n names.

Key Concepts

findFirst()Optional.map()orElse()ifPresent()

Approach 2: Chaining with flatMap

Java
import java.util.Optional; public class OptionalChaining { static class Address { private final String city; Address(String city) { this.city = city; } Optional<String> getCity() { return Optional.ofNullable(city); } } static class User { private final Address address; User(Address address) { this.address = address; } Optional<Address> getAddress() { return Optional.ofNullable(address); } } static String cityOf(User user) { return Optional.ofNullable(user) .flatMap(User::getAddress) // flatMap: the getter already returns an Optional .flatMap(Address::getCity) .orElse("unknown"); } public static void main(String[] args) { System.out.println(cityOf(new User(new Address("Pune")))); System.out.println(cityOf(new User(new Address(null)))); System.out.println(cityOf(new User(null))); System.out.println(cityOf(null)); } }

Output

Pune unknown unknown unknown

Core Logic

When getters themselves return Optionals, flatMap chains them, replacing a ladder of nested null checks.

How It Works
  1. 1Optional.ofNullable(user) starts the chain even if the user is null.
  2. 2flatMap(User::getAddress) and flatMap(Address::getCity) step through each level.
  3. 3If any level is empty, the rest is skipped and orElse("unknown") applies.
A user with no address, an address with no city, and a null user all return unknown.
💡

Key Point: Use map when a step returns a plain value and flatMap when it already returns an Optional — otherwise you get Optional&lt;Optional&lt;T&gt;&gt;.

Complexity
Time Complexity: O(1)Space Complexity: O(1)

Why: Each call walks a fixed number of levels.

Key Concepts

Optional.ofNullable()flatMap()safe navigation

Interview Questions

What's the difference between orElse and orElseGet?

orElse(value) always evaluates its argument, even when unused. orElseGet(supplier) computes the fallback only when the Optional is empty.

Should Optional be used for fields or method parameters?

Generally no. It's designed as a return type; for fields and parameters, plain values with clear null handling are preferred.

Learn the Concept

Related Programs