栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java8新特性 - Stream - 11 - Stream的allMatch()、anyMatch()和noneMatch()方法详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java8新特性 - Stream - 11 - Stream的allMatch()、anyMatch()和noneMatch()方法详解

1.方法介绍
【方法签名】
    1.boolean anyMatch(Predicate predicate);
    2.boolean allMatch(Predicate predicate);
    3.boolean noneMatch(Predicate predicate);
【方法属性】终结方法
【方法参数】函数式接口 Predicate,所以可以直接传入一个Lambda表达式
【方法作用】   
	1.boolean anyMatch : 任意一个元素满足条件,即返回true,否则返回false;
    2.boolean allMatch : 所有的元素都满足条件,才返回true,否则返回false;
    3.boolean noneMatch : 所有元素都不满足条件,才返回true,否则返回false;
【方法返回值】boolean 类型
2.案例代码 2.1 代码
package com.northcastle.I_stream;

import java.util.stream.Stream;

public class StreamTest10Match {
    public static void main(String[] args) {
        //0.准备一个数组
        String[] strs = {"aaa","bbb","ccc","ddddd","abcdefg"};
        // 1.判断所有元素是否都符合要求
        boolean aa = Stream.of(strs).allMatch(s -> s.startsWith("aa"));
        System.out.println("所有的元素都以 aa 开头 "+aa);
        //2.判断是否存在元素符合要求
        boolean aa1 = Stream.of(strs).anyMatch(s -> s.startsWith("aa"));
        System.out.println("存在某个元素以 aa 开头 "+aa1);
        //3.判断所有元素都不符合要求
        boolean aa2 = Stream.of(strs).noneMatch(s -> s.startsWith("aa"));
        System.out.println("所有的元素都不以 aa 开头 "+aa2);

    }
}

2.2 执行结果

3.完成

Congratulations!
You are one step closer to success!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763557.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号