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


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


DoubleStream anyMatch(DoublePredicate謂詞)返回此流的任何元素是否與提供的謂詞匹配。如果不一定要確定結果,則可能不會評估所有元素上的謂詞。這是短路端子操作。如果出現無限輸入時,端子操作可能會在有限時間內終止,則該端子操作會發生短路。
用法:

boolean anyMatch(DoublePredicate predicate)

參數:

  1. DoublePredicate : 一個雙值參數的謂詞(布爾值函數)。

返回值:如果流中的任何元素與提供的謂詞匹配,則該函數返回true,否則返回false。


注意:如果流為空,則返回false,並且不對該謂詞求值。

示例1:anyMatch()函數,用於檢查列表中的任何元素是否滿足給定條件。

// Java code for DoubleStream anyMatch 
// (DoublePredicate predicate) to check whether 
// any element of this stream match 
// the provided 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(1.2, 2.3, 
                                    3.4, 4.5, 5.6, 6.7); 
  
        // Stream anyMatch(DoublePredicate predicate) 
        boolean answer = stream.anyMatch(num -> (num - 5) > 0); 
  
        // Displaying the result 
        System.out.println(answer); 
    } 
}
輸出:
true

示例2:anyMatch()函數,用於檢查流中任何元素的平方根是否大於8。

// Java code for DoubleStream anyMatch 
// (DoublePredicate predicate) to check whether 
// any element of this stream match 
// the provided 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(10.3, 20.4, 30.5, 
                                              40.9, 50.4); 
  
        // Stream anyMatch(DoublePredicate predicate) 
        boolean answer = stream.anyMatch(num -> Math.sqrt(num) > 8); 
  
        // Displaying the result 
        System.out.println(answer); 
    } 
}
輸出:
false

示例3:anyMatch()函數,以顯示如果流為空,則返回false。

// Java code for DoubleStream anyMatch 
// (DoublePredicate predicate) to check whether 
// any element of this stream match 
// the provided predicate. 
import java.util.*; 
import java.util.stream.DoubleStream; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // Creating an empty DoubleStream 
        DoubleStream stream = DoubleStream.empty(); 
  
        boolean answer = stream.anyMatch(num -> true); 
  
        // Displaying the result 
        System.out.println(answer); 
    } 
}
輸出:
false


相關用法


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