Java Stream flatMapToDouble() with examples

 401 total views,  1 views today

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

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

Before learning flatMapToDouble() let us understand flattening?

What is flattening?

Flattening helps in converting a 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 flatMapToDouble() ?

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

It transforms the data as well as flatten the collection.

Method description

DoubleStream flatMapToDouble(Function<? super T,? extends DoubleStream> mapper)

As per official documentation,

Returns an DoubleStream 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 flatMapToDouble() is an intermediate operation it will have two characteristics.

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

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

flatMapToDouble() example

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

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

		List<List<Double>> list = new ArrayList<List<Double>>();
		List<Double> list1 = new ArrayList<>();
		list1.add(Double.valueOf(1));
		list1.add(Double.valueOf(2));

		List<Double> list2 = new ArrayList<>();
		list2.add(Double.valueOf(3));
		list2.add(Double.valueOf(4));

		List<Double> list3 = new ArrayList<>();
		list3.add(Double.valueOf(5));
		list3.add(Double.valueOf(6));

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

		list.stream()
		    .flatMapToDouble(item -> item.stream()
		    .mapToDouble(Double::new))
		    .forEach(System.out::println);

	}
}

Output

1.0
2.0
3.0
4.0
5.0
6.0

Conclusion

Java Stream flatMapToDouble() operation is mainly used for transforming and flattening a collectionflatMapToDoublet() will always create a new DoubleStream 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!.