📜  使用示例在Java中流式传输 allMatch()

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

使用示例在Java中流式传输 allMatch()

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

boolean allMatch(Predicate predicate)

Where, T is the type of the input to the predicate
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: allMatch()函数检查所有元素是否可被 3 整除。

// Java code for Stream allMatch
// (Predicate predicate) to check whether 
// all elements of this stream match 
// the provided predicate.
import java.util.*;
  
class GFG {
      
    // Driver code
    public static void main(String[] args) {
          
    // Creating a list of Integers
    List list = Arrays.asList(3, 4, 6, 12, 20);
      
    // Check if all elements of stream
    // are divisible by 3 or not using 
    // Stream allMatch(Predicate predicate)
    boolean answer = list.stream().allMatch(n-> n % 3 ==0);
      
    // Displaying the result
    System.out.println(answer);
}
}

输出 :

false

示例 2: allMatch()函数检查字符串的长度是否大于 2。

// Java code for Stream allMatch
// (Predicate predicate) to check whether 
// all elements of this stream match 
// the provided predicate.
import java.util.stream.Stream;
  
class GFG {
      
    // Driver code
    public static void main(String[] args) {
          
    // Creating a Stream of Strings
    Stream stream = Stream.of("Geeks", "for", 
                       "GeeksQuiz", "GeeksforGeeks");
          
    // Check if all elements of stream
    // have length greater than 2 using
    // Stream allMatch(Predicate predicate)
    boolean answer = stream.allMatch(str -> str.length() > 2);
      
    // Displaying the result
    System.out.println(answer);
}
}

输出 :

true

示例 3: allMatch()函数检查所有字符串是否在第一个索引处具有字符。

// Java code for Stream allMatch
// (Predicate predicate) to check whether 
// all elements of this stream match 
// the provided predicate.
import java.util.stream.Stream;
  
class GFG {
      
    // Driver code
    public static void main(String[] args) {
          
    // Creating a Stream of Strings
    Stream stream = Stream.of("Geeks", "for", 
                       "GeeksQuiz", "GeeksforGeeks");
          
    // Check if Character at 1st index is 
    // UpperCase in all strings or not using
    // Stream allMatch(Predicate predicate)
    boolean answer = stream.allMatch(str-> Character
                       .isUpperCase(str.charAt(1)));
      
    // Displaying the result
    System.out.println(answer);
}
}

输出 :

false

示例 4:使用同一流完成多个函数

// In case we want multiple function to be done.
  
import java.util.stream.IntStream;
  
public class MultipleStreamFunction {
  
    public static void main(String[] args) {
  
        final String sample = "Om Sarve Bhavantu Sukhinah";
  
        // converting to Ascii
        IntStream intstreams = sample.chars();
  
        // All match to check if all Ascii value greater then 100
        boolean answer = intstreams.allMatch(c -> c > 100);
        System.out.println(answer);
  
        // Need to initialize the stream again
        // to avoid runtime exception
        intstreams = sample.chars();
        // All match to check if all Ascii value greater then 31
  
        answer = intstreams.allMatch(c -> c > 31);
        System.out.println(answer);
  
    }
}

输出 :

false
true