Home  >  Forum  >  Core Java
Post New Query

Difference between map and flatMap in Java 8 Stream

joined on
April 01,2014
Asked on November 21,2014
In java 8 stream, there are two method map and flatMap.
Java doc says


Returns a stream consisting of the results of applying the given function to the elements of this stream.


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. Each mapped stream is closed after its contents have been placed into this stream

Unable to understand the difference between map and flatMap when should we use map and when flatMap?

joined on
October 10,2013
Replied on October 05,2016

Find the difference between Stream map() and flatMap() in Java 8.

1.       flatMap() flattens the elements after applying the function whereas  map() only apply function on the elements and does not flatten.

flatMap() works as follows.


a. Stream of writers.




      {10,"AAA"}, {20,"BBB"}





      {30,"XXX"}, {15,"ZZZ"}




b. After flatMap(writer -> writer.getBooks().stream()), find the stream of books.








Here the result has been flattened.


Now map() works as follows.


a. Stream of integer.


{2, 3, 4, 5}



b. After map(i -> i*2), find the stream of integer.


{4 6 8 10}


2.  Stream map() and flatMap()  both accepts a java 8 Function but Stream  map()  returns a single value where as flatMap() can return more than one values.

Example of flatMap()

    public static void main(String[] args) {

    List<Book> books = Arrays.asList(new Book(10, "AAA"), new Book(20, "BBB"));

    Writer w1 = new Writer("Mohan", books);

    books = Arrays.asList(new Book(30, "XXX"), new Book(15, "ZZZ"));

    Writer w2 = new Writer("Sohan", books);    

    List<Writer> writers = Arrays.asList(w1, w2);

    Book book = writers.stream().flatMap(writer -> writer.getBooks().stream())

    .max(new BookComparator()).get();

    System.out.println("Name:"+book.getName()+", Price:"+ book.getPrice() );



Example of map()

public static void main(String[] args) {

Map<Integer, String> map = new HashMap<>();

map.put(111, "Lalkrishna");

map.put(154, "Atal");

map.put(30, "Narendra");

map.put(200, "Amit");

List<User> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))

.map(e -> new User(e.getKey(), e.getValue())).collect(Collectors.toList());

list.forEach(l -> System.out.println("Id: "+ l.getId()+", Name: "+ l.getName()));


Find the link


Write Answer

Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us