Java DoubleStream noneMatch()示例

来源:这里教程网 时间:2026-02-17 20:49:40 作者:

Java DoubleStream noneMatch()示例

DoubleStream noneMatch(DoublePredicate predicate) 返回此流中是否没有元素与提供的谓词匹配。如果不是确定结果所必需的,它可能不会在所有元素上评估该谓词。这是一个 短路的终端操作。 如果一个终端操作在面对无限的输入时,可以在有限的时间内结束,那么它就是短路的。

语法

boolean noneMatch(DoublePredicate predicate)

其中,DoublePredicate表示一个谓词
(布尔值函数)的一个双值参数。

返回值: 如果流中的所有元素与提供的谓词相匹配或流为空,则该函数返回真,否则返回假。

注意: 如果流是空的,则返回true,并且不评估谓词。

例1: noneMatch()函数用于检查DoubleStream中是否没有元素能被5整除。

// Java code for DoubleStream noneMatch// (DoublePredicate predicate) to check whether// no 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(3.2, 5.0, 9.3, 12.4, 14.7);          // Check if no element of stream        // is divisible by 5 using        // DoubleStream noneMatch(DoublePredicate predicate)        boolean answer =             stream.noneMatch(num -> num % 5 == 0);          // Displaying the result        System.out.println(answer);    }}

输出:

false

例2: noneMatch()函数用于检查将两个DoubleStream串联后得到的DoubleStream中是否没有小于2的元素。

// Java code for DoubleStream noneMatch// (DoublePredicate predicate) to check whether// no 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 after        // concatenating two DoubleStreams        DoubleStream stream = DoubleStream.concat(            DoubleStream.of(3.3, 4.2, 5.1, 6.6),            DoubleStream.of(7.2, 8.3, 9.1, 10.5));          // Check if no element of stream        // is less than 2 using        // DoubleStream noneMatch(DoublePredicate predicate)        boolean answer = stream.noneMatch(num -> num < 2);          // Displaying the result        System.out.println(answer);    }}

输出:

true

例3: noneMatch()函数显示如果流是空的则返回true。

// Java code for DoubleStream noneMatch// (DoublePredicate predicate) to check whether// no 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();          // Using DoubleStream noneMatch() on empty stream        boolean answer = stream.noneMatch(num -> true);          // Displaying the result        System.out.println(answer);    }}

输出:

true

相关推荐