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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。