📜  Java中的 Stream noneMatch() 方法及示例

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

Java中的 Stream noneMatch() 方法及示例

流是支持各种方法的对象序列,这些方法可以流水线化以产生所需的结果。 Stream 类的noneMatch()返回此流的任何元素是否与提供的谓词匹配。如果不需要确定结果,它可能不会评估所有元素的谓词。这是一个短路端子操作。如果一个终端操作在有无限输入时可能会在有限时间内终止,那么它就是短路的。

句法:

boolean noneMatch(Predicate predicate)

其中,T 是谓词的输入类型,如果流中没有元素与提供的谓词匹配或流为空,则函数返回 true,否则返回 false。

现在我们将讨论几个示例,我们将在通过干净的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 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);
    }
}


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 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);
    }
}


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 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

示例 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 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 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