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


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


DoubleStream peek()是java.util.stream.DoubleStream中的方法。該函數返回由該流的元素組成的流,並在從結果流中消耗元素時對每個元素另外執行提供的操作。

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

用法:


DoubleStream peek(DoubleConsumer action) 

參數:

  1. DoubleStream : 一係列原始雙值元素。
  2. DoubleConsumer : 表示一個接受單個雙值參數並且不返回結果的操作。

返回值:該函數返回一個並行的DoubleStream。

注意:存在此方法主要是為了支持調試。

示例1:執行求和運算以找到給定DoubleStream的元素之和。

// Java code for DoubleStream peek() 
// where the action performed is to get 
// sum of all elements. 
import java.util.*; 
import java.util.stream.DoubleStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
  
        // Creating a stream of doubles 
        DoubleStream stream = 
                DoubleStream.of(2.2, 3.3, 4.5, 6.7); 
  
        // performing action sum on elements of 
        // given range and storing the result in sum 
        double sum = stream.peek(System.out::println).sum(); 
  
        // Displaying the result of action performed 
        System.out.println("sum is : " + sum); 
    } 
}
輸出:
2.2
3.3
4.5
6.7
sum is : 16.7

示例2:對給定DoubleStream的元素執行計數操作。

// Java code for DoubleStream peek() 
// where the action performed is to get 
// count of all elements in given range 
import java.util.*; 
import java.util.stream.DoubleStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
  
        // Creating a stream of doubles 
        DoubleStream stream = 
                  DoubleStream.of(2.2, 3.3, 4.5, 6.7); 
  
        // performing count operation on elements of 
        // given DoubleStream and storing the result in Count 
        long Count = stream.peek(System.out::println).count(); 
  
        // Displaying the result of action performed 
        System.out.println("count : " + Count); 
    } 
}
輸出:
2.2
3.3
4.5
6.7
count : 4

示例3:對給定DoubleStream的元素執行平均運算。

// Java code for DoubleStream peek() 
// where the action performed is to get 
// average of all elements. 
import java.util.*; 
import java.util.OptionalDouble; 
import java.util.stream.DoubleStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
  
        // Creating a stream of doubles 
        DoubleStream stream = 
                DoubleStream.of(2.2, 3.3, 4.5, 6.7); 
        ; 
  
        // performing action "average" on elements of 
        // given DoubleStream and storing the result in avg 
        OptionalDouble avg = stream.peek(System.out::println) 
                                 .average(); 
  
        // If a value is present, isPresent() 
        // will return true, else -1 is displayed. 
        if (avg.isPresent()) { 
            System.out.println("Average is : " + avg.getAsDouble()); 
        } 
        else { 
            System.out.println("-1"); 
        } 
    } 
}
輸出:
2.2
3.3
4.5
6.7
Average is : 4.175


相關用法


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