LongStream mapToInt() method in Java


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

Updated on: 30-Jul-2019

652 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements