IntStream asLongStream() method in Java


The asLongStream() method of the IntStream class returns a LongStream consisting of the elements of this stream, converted to long.

The syntax is as follows

LongStream asLongStream()

Create an IntStream and add some elements in the Stream

IntStream intStream = IntStream.of(30, 40, 60, 70, 90, 110, 140, 150);

Now, use the asLongStream() method to convert it to long

LongStream longStream = intStream.asLongStream();

The following is an example to implement IntStream asLongStream() method 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) {
      IntStream intStream = IntStream.of(30, 40, 60, 70, 90, 110, 140, 150);
      LongStream longStream = intStream.asLongStream();
      longStream.forEach(System.out::println);
   }
}

Output

30
40
60
70
90
110
140
150

Updated on: 30-Jul-2019

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements