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


Java DoubleStream boxed()用法及代碼示例


DoubleStream boxed()返回由該流的元素組成的Stream,每個元素都裝在Double中。

注意:DoubleStream boxed()是中間操作。這些操作總是很懶。在Stream實例上調用中間操作,並在完成處理後將中間實例作為輸出提供。
用法:

Stream<Double> boxed()

參數:


  1. Stream : 支持順序和並行聚合操作的一係列元素。
  2. Double : Double類將原始類型double的值包裝在對象中。類型為Double的對象包含一個類型為double的單個字段。

返回值:該函數將裝箱的流返回到Double。

示例1:

// Java code for DoubleStream boxed() 
import java.util.*; 
import java.util.stream.Stream; 
import java.util.stream.DoubleStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an DoubleStream 
        DoubleStream stream = DoubleStream.of(3.2, 8.4, 3.6, 4.7); 
  
        // Creating a Stream of Doubles 
        // Using DoubleStream boxed() to return 
        // a Stream consisting of the elements 
        // of this stream, each boxed to a Double. 
        Stream<Double> stream1 = stream.boxed(); 
  
        // Displaying the elements 
        stream1.forEach(System.out::println); 
    } 
}

輸出:

3.2
8.4
3.6
4.7

示例2:

// Java code for DoubleStream boxed() 
import java.util.*; 
import java.util.stream.Stream; 
import java.util.stream.DoubleStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an DoubleStream 
        DoubleStream stream = DoubleStream.of(3.2, 8.4, 3.6, 4.7); 
  
        // Creating a Stream of Doubles 
        // Using DoubleStream boxed() to return 
        // a Stream consisting of the elements 
        // of this stream, each boxed to a Double. 
        Stream<Double> stream1 = stream.boxed(); 
  
        Stream<Object> stream2 = Stream.concat(stream1, 
                                   Stream.of("Geeks", "for", "Geeks")); 
  
        // Displaying the elements 
        stream2.forEach(System.out::println); 
    } 
}

輸出:

3.2
8.4
3.6
4.7
Geeks
for
Geeks


相關用法


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