Bootstrap

Java中anyMatch()、allMatch()、noneMatch()用法详解

说明:
anyMatch():匹配到任何一个元素和指定的元素相等,返回 true
allMatch():匹配到全部元素和指定的元素相等,返回 true
noneMatch():与 allMatch() 效果相反

验证:
一、anyMatch()

1、正常匹配,多元素

List<String> strList = ListUtil.toList("a", "b", "c", "d");
        boolean a = Optional.ofNullable(strList).orElseGet(ListUtil::toList)
                .stream()
                .anyMatch(obj -> obj.equals("a"));
        System.out.println("anyMatch()测试多元素结果:" + a);

输出:anyMatch()测试多元素结果:true

2、正常匹配,单元素

List<String> strList = ListUtil.toList("a");
        boolean a = Optional.ofNullable(strList).orElseGet(ListUtil::toList)
                .stream()
                .anyMatch(obj -> obj.equals("a"));
        System.out.println("anyMatch()
;