A. 流是一系列支持各种方法的对象 可以通过管道输送来产生所需的结果。 不匹配() of Stream类返回此流的元素是否与提供的谓词匹配。如果不是确定结果所必需的,它可能不会对所有元素的谓词求值。这是一个 短路终端操作。 如果终端操作在无限输入时可能在有限时间内终止,则终端操作为短路。
null
提示: 它起作用了。对面 Stream anymatch()方法 .
语法:
boolean noneMatch(Predicate<? super T> predicate)
其中,T是谓词的输入类型,如果流中没有与提供的谓词匹配的元素,或者流为空,则函数返回true,否则返回false。
注: 如果流为空,则返回true,并且不计算谓词。
现在,我们将讨论几个例子,其中我们将介绍通过干净的java程序实现Stream noneMatch()方法时的不同场景,如下所示:
- 检查是否没有特定自定义长度的字符串
- 检查是否没有小于0的元素
- 检查所需位置是否没有具有所需字符的元素
例1: 检查是否没有长度为4的字符串。
JAVA
// Java Program to Illustrate noneMatch() method // of Stream class to check whether // no elements of this stream match the // provided predicate (Predicate predicate) // Importing Stream class from java.util.stream sub package import java.util.stream.Stream; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating a Stream of strings // Custom input strings are passed as arguments Stream<String> stream = Stream.of( "CSE" , "C++" , "Java" , "DS" ); // Now using Stream noneMatch(Predicate predicate) // and later storing the boolean answer as boolean answer = stream.noneMatch(str -> (str.length() == 4 )); // Printing the boolean value on the console System.out.println(answer); } } |
输出
false
例2: 检查是否没有小于0的元素。
JAVA
// Java Program to Illustrate noneMatch() method // of Stream class to check whether no elements // of this stream match the provided predicate // Importing required utility classes import java.util.*; // Main class class GFG { // amin driver method public static void main(String[] args) { // Creating a list of Integers using asList() of // Arrays class by declaring object of List List<Integer> list = Arrays.asList( 4 , 0 , 6 , 2 ); // Using Stream noneMatch(Predicate predicate) and // storing the boolean value boolean answer = list.stream().noneMatch(num -> num < 0 ); // Printing and displaying the above boolean value System.out.println(answer); } } |
输出
true
例3: 检查所需位置是否没有具有所需字符的元素。
JAVA
// Java Program to Illustrate noneMatch() method // of Stream class to check whether no elements of // this stream match the provided predicate // Importing required classes import java.util.stream.Stream; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating a Stream of Strings using of() method // by creating object of Stream of string type Stream<String> stream = Stream.of( "Geeks" , "fOr" , "GEEKSQUIZ" , "GeeksforGeeks" , "CSe" ); // Using Stream noneMatch(Predicate predicate) // and storing the boolean value boolean answer = stream.noneMatch( str -> Character.isUpperCase(str.charAt( 1 )) && Character.isLowerCase(str.charAt( 2 )) && str.charAt( 0 ) == 'f' ); // Printing the above boolean value on console System.out.println(answer); } } |
输出
false
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END