Java Stream mapToDouble() with examples

 469 total views,  1 views today

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

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

mapToDouble() operation will transform each element of the stream to Double form according to the provided function. It returns a DoubleStream

Method description

DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)

As per official documentation

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

mapToDouble() accepts a ToDoubleFunction interface.A ToDoubleFunction interface takes an input and returns a value of Double type.

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

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

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

Java stream mapToDouble() example

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

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1.0");
		list.add("2.0");
		list.add("3.0");
		list.add("4.0");
		list.add("5.0");
		DoubleStream doubleList = list.stream()
		            .mapToDouble(item -> Double.valueOf(item));
	}
}

Java stream mapToDouble() example to convert Stream of String to Double

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1.0");
		list.add("2.0");
		list.add("3.0");
		list.add("4.0");
		list.add("5.0");
		list.stream()
		    .mapToDouble(item -> Double.valueOf(item))
		    .forEach(System.out::println);
	}
}

Output

1.0
2.0
3.0
4.0
5.0

Java stream mapToDouble() with ToDoubleFunction interface example

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		list.add("5");
		ToDoubleFunction<String> function = (String item) -> Double.valueOf(item);

		list.stream()
		    .mapToDouble(function)
		    .forEach(System.out::println);
	}
}

Conclusion

Java Stream mapToDouble() operation is mainly used for transforming one form of the stream to Double form. mapToDouble() 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!.