Open links in new tab
    • Work Report
    • Email
    • Rewrite
    • Speech
    • Title Generator
    • Smart Reply
    • Poem
    • Essay
    • Joke
    • Instagram Post
    • X Post
    • Facebook Post
    • Story
    • Cover Letter
    • Resume
    • Job Description
    • Recommendation Letter
    • Resignation Letter
    • Invitation Letter
    • Greeting Message
    • Try more templates
  1. In Java, flatMap() is a Stream API method introduced in Java 8 that transforms each element of a stream into another stream and then flattens the resulting streams into a single stream. It’s particularly useful for one-to-many transformations and for avoiding nested structures like Stream<Stream<T>>.

    Key difference from map():

    • map() → One-to-one mapping: each element maps to exactly one result.

    • flatMap() → One-to-many mapping: each element maps to zero or more results, and the results are flattened into a single stream.

    Example – Flattening a Stream of Lists

    import java.util.*;
    import java.util.stream.*;

    public class FlatMapExample {
    public static void main(String[] args) {
    List<List<String>> listOfLists = Arrays.asList(
    Arrays.asList("Geeks", "For"),
    Arrays.asList("Java", "Programming")
    );

    List<String> flatList = listOfLists.stream()
    .flatMap(Collection::stream) // flatten each inner list
    .collect(Collectors.toList());

    System.out.println(flatList); // [Geeks, For, Java, Programming]
    }
    }
    Copied!
    Feedback
  2. Java Stream flatMap () with Examples - HowToDoInJava

    • Imagine we have a bunch of boxes, and each box contains some items. Now, we want to take out all those items from the boxes and put them in a single box. That’s what flatMap() does with a stream. It takes objects from different collections in this stream A and puts all objects in a new Stream B. In layman’s terms, flattening is referred to as mergi...
    See more on howtodoinjava.com
  3. Java flatMap Explained with Examples

    If map() transforms data, then flatMap() flattens it. It may sound technical, but in real-world Java code, flatMap() solves very practical problems — especially when working with nested collections, optional …

  4. The Difference Between map () and flatMap () - Baeldung

    Nov 10, 2025 · Learn about the differences between map () and flatMap () by analyzing some examples of Streams and Optionals.

  5. Java 8 flatMap example - Mkyong.com

    Apr 14, 2016 · This example uses .stream() to convert a List into a stream of objects, and each object contains a set of books, and we can use flatMap to produces a …

  6. Java 8 Streams FlatMap method example - Stack Overflow

    The Function passed to flatMap is responsible for creating the Stream, so the example I give is really shorthand for integerListStream .flatMap(ints -> ints.stream()) where ints are the lists of integers from …

  7. Java 8 Streams FlatMap Method Example: How to Use flatMap() in …

    Dec 18, 2025 · Unlike map(), which transforms elements one-to-one, flatMap() "flattens" nested streams into a single stream, making it indispensable for simplifying complex data processing workflows. This …

  8. Java’s flatMap () Method Explained - Medium

    Oct 23, 2024 · In this article, we will explore the basics of how flatMap() works, when to use it, and provide some practical examples to showcase its usefulness in …

  9. Java Stream flatMap - Flattening Streams in Java - ZetCode

    May 24, 2025 · This article demonstrates how to use the Java Stream flatMap method to transform and flatten nested data structures. flatMap is an intermediate stream operation that maps each element …

  10. Java Stream map () vs. flatMap () with Examples

    Aug 27, 2023 · The difference between map () vs flatMap () with example. map () is used for transformation only, but flatMap () is used for both transformation and …