Java Stream mapToLong() with examples

100 total views, 1 views today

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

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

mapToLong() operation will transform each element of the stream to Long form according to the provided function. It returns a LongStream.

Method description

LongStream mapToLong(ToLongFunction<? super T> mapper)

As per official documentation

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

mapToLong() accepts a ToLongFunction interface.A ToLongFunction interface takes an input and returns a value of Long type.

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

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

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

Java stream mapToLong() example

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

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");
		LongStream longList = list.stream()
                         .mapToLong(item -> Long.valueOf(item));
	}
}

Java stream mapToLong() example to convert Stream of String to Long

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");
		list.stream().mapToLong(item -> Long.valueOf(item))
                    .forEach(System.out::println);
	}
}

Output

1
2
3
4
5

Java stream mapToLong() with ToLongFunction interface example

import java.util.ArrayList;
import java.util.List;
import java.util.function.ToLongFunction;

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");
		ToLongFunction<String> function = (String item) -> 
                                               Long.valueOf(item);
		   
         list.stream().mapToLong(function).forEach(System.out::println);
	}
}

Conclusion

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.