Saturday, December 13, 2025
HomeLanguagesJavaIntStream asLongStream() in Java

IntStream asLongStream() in Java

IntStream asLongStream() returns a LongStream consisting of the elements of this stream, converted to long. This is an intermediate operation. Intermediate operations are invoked on a Stream instance and after they finish their processing, they give a Stream instance as output.

Syntax :

LongStream asLongStream()

Where, LongStream is a sequence of 
primitive long-valued element.

Return Value : IntStream asLongStream() returns a LongStream consisting of the elements of this stream, converted to long.

Example 1 :




// Java code for LongStream asLongStream()
// to return a LongStream consisting of
// the elements of this stream
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream = IntStream.of(3, 5, 9, 12, 14);
  
        // Using LongStream asLongStream()
        LongStream stream1 = stream.asLongStream();
  
        // Displaying LongStream consisting of
        // the elements of this stream
        stream1.forEach(System.out::println);
    }
}


Output :

3
5
9
12
14

Example 2 :




// Java code for LongStream asLongStream()
// to return a LongStream consisting of
// the elements of this stream
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream and using asLongStream()
        LongStream stream = IntStream.range(3, 8).asLongStream();
  
        // Displaying LongStream consisting of
        // the elements of this stream
        stream.forEach(System.out::println);
    }
}


Output :

3
4
5
6
7
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32447 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6816 POSTS0 COMMENTS
Nicole Veronica
11953 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6951 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6898 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS