Sitemap
Learn

Learn

On a continuing learning journey..

Core Java:Optional flatmap method

2 min readAug 12, 2025

Optional has the flatMap method which can potentially help better than map when working with nested object structures.

The following flatMap focused idiom works well for all scenarios irrespective of at what level the value is missing.

Optional.ofNullable(full)
.flatMap(emp -> Optional.ofNullable(emp.getDepartment()))
.flatMap(dept -> Optional.ofNullable(dept.getName()))
.orElse("department name not found")
;

This works when both Employee and Department are not returning Optionals.

The above flatMap style idiom would work when the methods are not returning Optionals.

Get Learn’s stories in your inbox

Join Medium for free to get updates from this writer.

Another example with much more nesting as below.

package com.core.java.functional;

import java.util.Optional;

public class Citizen {
class Continent {
public Continent(String name) {
this.name = name;
}

public String name;
public Country country;
}

class Country {
public Country(String name) {
this.name = name;
}
public String name;
public State state;
}

class State {
public State(String name) {
this.name = name;
}

public String name;
public City city;
}

class City {
public City(String name) {
this.name = name;
}

public String name;
public Suburb suburb;
}

class Suburb {
public Suburb(String name) {
this.name = name;
}

public String name;
public Community community;
}

class Community {
public Community(String name) {
this.name = name;
}

public String name;
public Address address;
}

class Address {
public Address(String name) {
this.name = name;
}

public String name;
}

public static void main(String[] args) {
Citizen citizen = new Citizen();
citizen.citizenTest();
}

public void citizenTest() {
Continent america = new Continent("america");
Country country1 = new Country("america");
State state1 = new State("Georgia");
City city1 = new City("atlanta");
Suburb suburb1 = new Suburb("alpharetta");
Community community1 = new Community("duck pond");
Address address1 = new Address("703drive");
america.country=country1;
country1.state = state1;
state1.city = city1;
city1.suburb = suburb1;
suburb1.community = community1;
community1.address = address1;

String addressName = Optional.ofNullable(america)
.flatMap(continent -> Optional.ofNullable(continent.country))
.flatMap(country -> Optional.ofNullable(country.state))
.flatMap(state -> Optional.ofNullable(state.city))
.flatMap(city -> Optional.ofNullable(city.suburb))
.flatMap((suburb -> Optional.ofNullable(suburb.community)))
.flatMap(community -> Optional.ofNullable(community.address))
.flatMap(address -> Optional.ofNullable(address.name))
.orElse("address name not present")
;

System.out.println("addressName = " + addressName);
}
}
addressName = 703drive

If the wiring is disconnected at any level with a comment as below, the addressName is rightly returned as “address name not present”

        Continent america = new Continent("america");
Country country1 = new Country("america");
State state1 = new State("Georgia");
City city1 = new City("atlanta");
Suburb suburb1 = new Suburb("alpharetta");
Community community1 = new Community("duck pond");
Address address1 = new Address("703drive");
america.country=country1;
country1.state = state1;
state1.city = city1;
city1.suburb = suburb1;
// suburb1.community = community1;
community1.address = address1;

Result

addressName = address name not present

--

--

Learn
Learn

Written by Learn

On a continuing learning journey..