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


Java DoubleStream filter()用法及代码示例


DoubleStream filter(DoublePredicate predicate)返回包含与给定谓词匹配的该流元素的流。这是一个中间操作。这些操作总是很懒惰,即执行诸如filter()之类的中间操作实际上并不执行任何过滤,而是创建一个新的流,该新流在遍历时将包含与给定谓词匹配的初始流的元素。

用法:

DoubleStream filter(DoublePredicate predicate)

参数:


  1. DoubleStream : 一系列原始双值元素。
  2. DoublePredicate : 一个双值参数的谓词(布尔值函数)。

返回值:该函数返回新的流。

示例1:DoubleStream上的filter()方法。

// Java code for DoubleStream filter 
// (DoublePredicate predicate) to get a stream 
// consisting of the elements of this 
// stream that match the given predicate. 
import java.util.*; 
import java.util.stream.DoubleStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an DoubleStream 
        DoubleStream stream = DoubleStream.of(3.6, 5.4, 6.6, 8.1, 9.7); 
  
        // Using DoubleStream filter(DoublePredicate predicate) 
        // to get a stream consisting of the 
        // elements that are greater than 5.7 
        stream.filter(num -> num > 5.7) 
            .forEach(System.out::println); 
    } 
}
输出:
6.6
8.1
9.7

示例2:DoubleStream上的filter()方法。

// Java code for DoubleStream filter 
// (DoublePredicate predicate) to get a stream 
// consisting of the elements of this 
// stream that match the given predicate. 
import java.util.*; 
import java.util.stream.DoubleStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an DoubleStream 
        DoubleStream stream = DoubleStream.of(3.6, 5.4, 6.6, 8.1, 9.7); 
  
        // Using DoubleStream filter(DoublePredicate  
        // predicate) to get a stream consisting of  
        // the elements that when divided by 2 
        // gives quotient > 2.3 
        stream.filter(num -> num / 2.0 > 2.3) 
            .forEach(System.out::println); 
    } 
}
输出:
5.4
6.6
8.1
9.7


相关用法


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