Skip to content

Latest commit

 

History

History
41 lines (26 loc) · 1.26 KB

File metadata and controls

41 lines (26 loc) · 1.26 KB

Java map() vs flatMap()

You know map(), but do you really get flatMap()? Learn how they differ, when flattening matters, and how to transform collections like a pro — with full examples. Read more →

Original Article

Read the complete tutorial with detailed explanations here:
https://www.javapro.academy/java-map-vs-flatmap/

What's Inside

This repository includes working Java examples demonstrating:

  • How map() transforms values one-to-one with automatic wrapping
  • The nested structure problem (Optional<Optional<String>>, Stream<Stream<T>>)
  • How flatMap() flattens nested Optional and Stream hierarchies
  • Choosing between map() and flatMap() based on return types
  • Chaining multiple flatMap() calls for database lookups and API calls
  • Flattening nested collections into a single stream

Prerequisites

  • Java 17 or higher

Running the Examples

Clone the repository:

git clone https://github.com/The-Java-Bootcamp/java-map-vs-flatmap.git
cd java-map-vs-flatmap

Run any example directly from your IDE (IntelliJ IDEA, Eclipse, VS Code). Each class has its own main method.

Learn More

Visit JavaPro Academy for more Java tutorials and courses:
https://www.javapro.academy/

Last Updated: February 17, 2026