📜  Java中的 IntStream noneMatch() 示例

📅  最后修改于: 2022-05-13 01:55:43.345000             🧑  作者: Mango

Java中的 IntStream noneMatch() 示例

IntStream noneMatch(IntPredicate predicate)返回此流的任何元素是否与提供的谓词匹配。如果不需要确定结果,它可能不会评估所有元素的谓词。这是一个短路端子操作。如果一个终端操作在有无限输入时可能会在有限时间内终止,那么它就是短路的。
句法 :

boolean noneMatch(IntPredicate predicate)

Where, IntPredicate represents a predicate (boolean-valued function)
of one int-valued argument and the function returns true if either
all elements of the stream match the provided predicate or 
the stream is empty, otherwise false.

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

示例 1: noneMatch()函数检查 IntStream 的任何元素是否可被 5 整除。

// Java code for IntStream noneMatch
// (Predicate predicate) to check whether
// no element of this stream match
// the provided predicate.
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating an IntStream
        IntStream stream = IntStream.of(3, 5, 9, 12, 14);
  
        // Check if no element of stream
        // is divisible by 5 using
        // IntStream noneMatch(Predicate predicate)
        boolean answer = stream.noneMatch(num -> num % 5 == 0);
  
        // Displaying the result
        System.out.println(answer);
    }
}
输出:
false

例2: noneMatch()函数检查两个IntStream拼接后得到的IntStream中是否没有元素小于2。

// Java code for IntStream noneMatch
// (Predicate predicate) to check whether
// no element of this stream match
// the provided predicate.
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream after concatenating
        // two IntStreams
        IntStream stream = IntStream.concat(IntStream.of(3, 4, 5, 6),
                                            IntStream.of(7, 8, 9, 10));
  
        // Check if no element of stream
        // is less than 2 using
        // IntStream noneMatch(Predicate predicate)
        boolean answer = stream.noneMatch(num -> num < 2);
  
        // Displaying the result
        System.out.println(answer);
    }
}
输出:
true

示例 3: noneMatch()函数显示流是否为空,则返回 true。

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