Java 8 Stream flatMap

Java 8 Stream flatmap

In this post, we will see about Java 8 Stream flatMap function.Before understanding flatMap, you should go through stream’s map function

Stream‘s flatMap method takes single element from input stream and produces any number of output values and flattens result to output stream.When you apply flatMap function on each of element of the stream, it results in stream of values rather than single value as in case of Stream’s map function.

Java 8 Stream flatMap function

As per java docs, Stream’s flatMap returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.

Confused? Don’t worry, example will make it very clear.
Function is a functional interface available in Java 8 java.util.function.Function accepts one input and produce a result.
Let’s understand with the help of a simple example.

Java 8 Stream flatMap example

Let’s say you have an employee class. Each employee has list of cities where they have lived in past.You need to find list of all cities where Employees have lived in.

For example:
Let’s say Ramesh has lived in Delhi, Mumbai and Rekha has lived in Pune, Delhi, then the output will be Delhi, Mumbai, Pune.

So we will use flatMap function to map each employee to list of cites to flatten it and then collection it in form of set.

  1. Create a class named "Employee.java"

    }

    1. Create main class named "Java8StreamMapMain.java"

      }

      When you run above program, you will get below output:

      Delhi
      Patna
      Banglore
      Kolkata
      Pune
      Mumbai

      Another example

      Let’s say you have 2D array of integers and you need to create a flattened list of integers after doubling each element.

      When you run above program, you will get below output:

      [2, 4, 6, 8, 10, 12]

      If you notice, we have used flatMap to flatten the 2D array and then used map function to double of input number in above example.

      That’s all about Java 8 Java 8 stream flatMap example.


      import_contacts

      You may also like:

Related Posts

  • Find duplicate elements in the Stream
    17 October

    Java 8 – Find duplicate elements in Stream

    Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()Conclusion Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, and one of […]

  • Format Instant to String in java
    02 May

    How to format Instant to String in java

    Learn about how to format Instant to String in java.

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.Date In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    Table of ContentsUsing Instant objectUsing java.sql.Date In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which […]

  • Convert Stream to List in java
    31 December

    Java Stream to List

    Table of ContentsUsing Collectors.toList()Using Collectors.toCollection()Using foreachFilter Stream and convert to ListConvert infinite Stream to List In this post, we will see how to convert Stream to List in java. There are multiple ways to convert Stream to List in java. Using Collectors.toList() You can pass Collectors.toList() to Stream.collect() method to convert Stream to List in […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    Table of ContentsLocalDateTimeTimestampConvert LocalDateTime to Timestamp In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.