当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java IntStream asLongStream()用法及代码示例


IntStream asLongStream()返回包含此流元素的LongStream,并将其转换为long。这是一个中间操作。在Stream实例上调用中间操作,并在完成处理后将中间实例作为输出提供。

用法:

LongStream asLongStream()

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

返回值:IntStream asLongStream()返回包含此流元素的LongStream,并将其转换为long。


示例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); 
    } 
}

输出:

3
5
9
12
14

示例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); 
    } 
}

输出:

3
4
5
6
7


相关用法


注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 IntStream asLongStream() in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。