Java Stream flatmap() with examples

 216 total views,  1 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream flatmap() with examples.

flatmap() is a part of the Stream pipeline and an intermediate operation. It belongs to the Stream interface.

Before learning flatmap() let us understand flattening?

What is flattening?

Flattening helps in converting collection of several list or collections into a single collection. It merges all list of collections to form a single collection.

Flattening =  Collection<Collection <T>> to Collection<T>

Examples

[[a,b,c],[d,e,f],[g,h],[i,j,k,l,m] = [a,b,c,d,e,f,g,h,i,j,k,l,m]
It flatten out several lists of data into single list.

What is flatmap() ?

flatmap() flattens the stream of collections to form a single stream.It uses map() + flatten() operations on the input stream.

It transforms the data as well as flatten the collection.

Method description

<R> Stream<R> flatMap(Function<? super T,? extends Stream<? extends R>> mapper)

As per official documentation,

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.

Since flatmap() is an intermediate operation it will have two characteristics.

  1. flatmap() is lazy loading operation. This means data will only be mapped and flattened when a terminal operation is invoked on the stream.
  2. flatmap() operation on a stream will always create a new stream output.

Let us now see a few examples of Java 8 stream flatmap()

flatmap() example

In this example, we have a list of strings and flatmap() operation is applied on the list to convert it into a stream of letters.

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

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

		List<String> list = new ArrayList<>();
		list.add("apple");
		list.add("orange");

                list.stream()
		    .flatMap(item -> Stream.of(item.split("")))
		    .forEach(System.out::println);


	}
}

Output

a
p
p
l
e
o
r
a
n
g
e

flatmap() example 2

package com.test;

import java.util.ArrayList;
import java.util.List;

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

		List<List<String>> list = new ArrayList<List<String>>();
		List<String> list1 = new ArrayList<>();
		list1.add("apple");
		list1.add("orange");

		List<String> list2 = new ArrayList<>();
		list2.add("banana");
		list2.add("grapes");

		List<String> list3 = new ArrayList<>();
		list3.add("cherry");
		list3.add("dragonfruit");

		list.add(list1);
		list.add(list2);
		list.add(list3);

		list.stream()
		    .flatMap(item -> item.stream())
		    .forEach(System.out::println);

	}
}

Output

apple
orange
banana
grapes
cherry
dragonfruit

Conclusion

Java Stream flatmap() operation is mainly used for transforming and flattening a collection. flatmap() will always create a new stream and it is lazily evaluated.

References : Official documentation

Thanks for reading. Please a comment and give us a thumbs up! 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.