IntStream mapToLong() method in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Updated on 30-Jul-2019 22:30:25

Advertisements