Java 8 - Stream flatMap() method example

Posted on April 29, 2017


The Stream#flatMap() method consumes a single value from input stream and produces any number of output values, and then flatten the resulting values to new stream.

For example -

Stream[ [1, 2], [3, 4], [5, 6]]  ⇒ flatMap(list → list.stream()) ⇒ Stream[1, 2, 3, 4, 5, 6]

Stream[ ["Java", "Scala"], ["Php", "C#"]] ⇒ flatMap(user → user.getLanguages() ) ⇒  Stream["Java", "Scala", "Php", "C#"]

You may also like - Java 8 - Stream map() method example

The following examples demonstrate how the flatMap() method works in java program. 

Example #1

Here is an example of using the map() and flatMap() methods of  the Stream object with List.

StreamFlatMapExample1.java

package com.boraji.tutorial.stream;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author imssbora
 *
 */
public class StreamFlatMapExample1 {
   public static void main(String[] args) {


      System.out.println("-----------------Using map-----------------");
      List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
      numbers.stream().map(n -> n * 2)          // return Stream<Integer>
            .forEach(System.out::println);

      System.out.println("-----------------Using flatMap-----------------");
      List<Integer> numbers1 = Arrays.asList(1, 2);
      List<Integer> numbers2 = Arrays.asList(4, 5);
      List<Integer> numbers3 = Arrays.asList(7, 8);

      Stream.of(numbers1, numbers2, numbers3)    // return Stream<List<Integer>>
            .flatMap(list -> list.stream())      // return Stream<Integer>
            .map(n -> n * 2)                     // return Stream<Integer>
            .forEach(System.out::println);

   }
}

Output

-----------------Using map-----------------
2
4
6
8
10
-----------------Using flatMap-----------------
2
4
8
10
14
16

Example #2

Here is an example of using the map() and flatMap() method of the Stream object with the Person’s list.

Person.java

public class Person {
   private String name;
   private List<String> languages;

   public Person(String name, List<String> languages) {
      this.name = name;
      this.languages = languages;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public List<String> getLanguages() {
      return languages;
   }

   public void setLanguages(List<String> languages) {
      this.languages = languages;
   }
}

StreamFlatMapExample2.java 

package com.boraji.tutorial.stream;

import java.util.Arrays;
import java.util.List;

/**
 * @author imssbora
 *
 */
public class StreamFlatMapExample2 {
   public static void main(String[] args) {

      Person person1 = new Person("Mike", Arrays.asList("Java", "Scala"));
      Person person2 = new Person("Devid", Arrays.asList("Php", "JavaScript"));
      Person person3 = new Person("Mohit", Arrays.asList("C++", "C#"));

      List<Person> persons = Arrays.asList(person1, person2, person3);

      System.out.println("--------Using map() method to print name of all person---------");
      persons.stream()                    // return Stream<Person>
            .map(p -> p.getName())        // return Stream<String>
            .forEach(System.out::println);

      System.out.println("\n--------Using flatMap() method to print all languages--------");
      persons.stream()                    // return Stream<Person>
            .map(p -> p.getLanguages())   // return Stream<List<String>>
            .flatMap(l -> l.stream())     // return Stream<String>
            .forEach(System.out::println);
   }
}

Output

--------Using map() method to print name of all person---------
Mike
Devid
Mohit

--------Using flatMap() method to print all languages--------
Java
Scala
Php
JavaScript
C++
C#