Java Stream allMatch() 示例

Java Stream allMatch() 示例

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

语法:

boolean allMatch(Predicate <? super T> predicate)

其中,T是输入到谓词的类型
并且该函数在以下情况下返回真:如果流的所有元素
匹配所提供的谓词或
流是空的,否则为假。

注意: 如果流是空的,那么返回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<Integer> 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<String> 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()函数用于检查所有字符串是否在第一个索引处有UpperCase字符。

// 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<String> 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 than 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 than 31
 
        answer = intstreams.allMatch(c -> c > 31);
        System.out.println(answer);
 
    }
}

输出:

false
true

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程