LongStream mapToInt() method in Java

Java 8Object Oriented ProgrammingProgramming

The mapToInt() method returns an IntStream consisting of the results of applying the given function to the elements of this stream.

The syntax is as follows

mapToInt(LongToIntFunction mapper)

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

Declare LongStream and add some elements

LongStream longStream = LongStream.of(1000L, 13000L, 18000L);

Now, use the IntStream and mapToInt()

IntStream intStream = longStream.mapToInt(val -> (int) val);

The following is an example to implement LongStream mapToInt() in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(1000L, 13000L, 18000L);
      IntStream intStream = longStream.mapToInt(val -> (int) val);
      intStream.forEach(System.out::println);
   }
}

Output

1000
13000
18000
raja
Published on 28-Mar-2019 16:13:22
Advertisements