IntStream mapToLong() method in Java


The mapToLong() function in IntStream class returns a LongStream consisting of the results of applying the given function to the elements of this stream.

The syntax is as follows.

LongStream mapToLong(IntToLongFunction mapper)

Here, the parameter mapper is a stateless function to apply to each element.

Create an IntStream with some elements in the Stream.

IntStream intStream = IntStream.of(50, 100, 150, 200);

Now create a LongStream and use the mapToLong() with a condition.

LongStream longStream = intStream.mapToLong(num → (long)num);

The following is an example to implement IntStream mapToLong() method in Java.

Example

import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(50, 100, 150, 200);
      LongStream longStream = intStream.mapToLong(num → (long)num);
      longStream.forEach(System.out::println);
   }
}

Output

50
100
150
200

Updated on: 30-Jul-2019

475 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements