首页 > 文章列表 > Java中的Match用法详解

Java中的Match用法详解

java Match
260 2023-05-03

概念

1、各种Match操作可用于判断给定的Predicate是否符合Stream的要素。

2、Match操作是终端操作,返回布尔值。

实例

boolean anyStartsWithA =
    stringCollection
        .stream()
        .anyMatch((s) -> s.startsWith("a"));
 
System.out.println(anyStartsWithA);      // true
 
boolean allStartsWithA =
    stringCollection
        .stream()
        .allMatch((s) -> s.startsWith("a"));
 
System.out.println(allStartsWithA);      // false
 
boolean noneStartsWithZ =
    stringCollection
        .stream()
        .noneMatch((s) -> s.startsWith("z"));
 
System.out.println(noneStartsWithZ);      // true