Java 8 Map vs flatmap method
Tags: java, flatmap, map, flatmapvsmap
Published: 2021-01-20 10:09:44

<hr />

<h3>Map vs FlatMap:</h3>
Java 8 stream API provides <strong>map()</strong> and <strong>flatMap()</strong> methods. Both these methods are intermediate methods and returns another stream as part of the output.
<ul>
 	<li><strong>map()</strong> method use for transformation</li>
 	<li><strong>flatMap()</strong> used for transformation &amp; flattering</li>
 	<li><strong>flatMap()</strong>-&gt; <strong>map()</strong>+<strong>flatring</strong></li>
</ul>
<h4>map():</h4>
<ul>
 	<li><strong>map()</strong> takes Stream&lt;T&gt; as input and return Stream&lt;R&gt;</li>
 	<li>Stream&lt;R&gt; map (String&lt;T&gt; input)</li>
 	<li>it's mapper function produces single value for each input value. hence it also called as one-to-one mapping.</li>
</ul>
<h4>flatMap():</h4>
<ul>
 	<li><strong>flatMap()</strong> takes Stream&lt;Stream&lt;T&gt;&gt; as input and return Stream&lt;R&gt;</li>
 	<li>Stream&lt;R&gt; map (String&lt;T&gt; input)</li>
 	<li>it's mapper function produces multiple value for each input value. hence it also called as one-to-many mapping.</li>
</ul>
<table>
<thead>
<tr>
<th>map()</th>
<th>flatMap()</th>
</tr>
</thead>
<tbody>
<tr>
<td>It processes stream of values.</td>
<td>It processes stream of stream of values.</td>
</tr>
<tr>
<td>It does only mapping.</td>
<td>It performs mapping as well as flattening.</td>
</tr>
<tr>
<td>It’s mapper function produces single value for each input value.</td>
<td>It’s mapper function produces multiple values for each input value.</td>
</tr>
<tr>
<td>It is a One-To-One mapping.</td>
<td>It is a One-To-Many mapping.</td>
</tr>
<tr>
<td>Data Transformation : From Stream to Stream</td>
<td>Data Transformation : From Stream&lt;Stream to Stream</td>
</tr>
<tr>
<td>Use this method when the mapper function is producing a single value for each input value.</td>
<td>Use this method when the mapper function is producing multiple values for each input value.</td>
</tr>
</tbody>
</table>
Let's See the below example for better understanding
<h4>Employee.java</h4>
<pre class="EnlighterJSRAW" data-enlighter-language="java">import java.util.List;

public class Employee {

    private int id;
    private String name;
    private String email;
    private List&lt;String&gt; mobileNumbers;

    public Customer() {
    }

    public Employee(int id, String name, String email, List&lt;String&gt; mobileNumbers) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.mobileNumbers = mobileNumbers;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public List&lt;String&gt; getMobileNumbers() {
        return mobileNumbers;
    }

    public void setMobileNumbers(List&lt;String&gt; mobileNumbers) {
        this.mobileNumbers = mobileNumbers;
    }
}</pre>
<h4>Repository.java</h4>
<pre class="EnlighterJSRAW" data-enlighter-language="java">import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Repository {


    public static List&lt;Employee&gt; getAll() {
        return Stream.of(
                new Employee(001, "Nadeem", "Nadeem@localhost.com", Arrays.asList("737937955", "9812356")),
                new Employee(002, "Shyam", "Shyam@localhost.com", Arrays.asList("65563865", "987654")),
                new Employee(003, "Punneet", "Punneet@localhost.com", Arrays.asList("9412345", "43256")),
                new Employee(004, "Kailash", "Kailash@localhost.com", Arrays.asList("9812345678", "1234456546"))
        ).collect(Collectors.toList());
    }

}</pre>
<h4>MapFlatMapExample.java</h4>
<pre class="EnlighterJSRAW" data-enlighter-language="java">import java.util.List;
import java.util.stream.Collectors;

public class MapFlatMapExample {
    public static void main(String[] args) {

        List&lt;Employee&gt; employee = Repository.getAll();

        List&lt;String&gt; emails = employee.stream()
                .map(employee -&gt; employee.getEmail())
                .collect(Collectors.toList());
        System.out.println(emails);
        List&lt;List&lt;String&gt;&gt; mobileNumbers = customers.
                stream().map(employee -&gt; employee.getMobileNumbers())
                .collect(Collectors.toList());
        System.out.println(mobileNumbers);

        List&lt;String&gt; mobiles = customers.stream()
                .flatMap(employee -&gt; employee.getMobileNumbers().stream())
                .collect(Collectors.toList());
        System.out.println(mobiles);
    }
}</pre>
<strong>Output:</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="generic">Nadeem@localhost.com,Shyam@localhost.com,Punneet@localhost.com,Kailash@localhost.com

737937955,9812356,65563865,987654,9412345,43256,9812345678,1234456546</pre>
&nbsp;

<hr />