當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java LongStream asDoubleStream()用法及代碼示例


LongStream asDoubleStream()返回包含此流元素的DoubleStream,並將其轉換為double。這是一個中間操作。在Stream實例上調用中間操作,並在完成處理後將中間實例作為輸出提供。

用法:

DoubleStream asDoubleStream()

Where, DoubleStream is a sequence of 
primitive double-valued element.

返回值:LongStream asDoubleStream()返回包含此流元素的DoubleStream,並將其轉換為double。


示例1:

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

輸出:

3.0
5.0
9.0
12.0
14.0

示例2:

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

輸出:

3.0
4.0
5.0
6.0
7.0

示例3:

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

輸出:

3
3.0
4
4.0
5
5.0
6
6.0
7
7.0


相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 LongStream asDoubleStream() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。