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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。