编程语言
首页 > 编程语言> > Java8 stream-anyMath\allMatch\noneMatch总结

Java8 stream-anyMath\allMatch\noneMatch总结

作者:互联网

package stream;

import java.util.Arrays;
import java.util.List;

/**
 * @author zzl
 * @Date 2022/1/15
 * @description Java stream特性
 */
public class StreamTest {
    public static void main(String[] args) {
        List<String> resultList = Arrays.asList("a", "b", "c");

        // allMatch:判断集合中的所有元素都是满足条件,返回true
        boolean allMatch = resultList.stream().allMatch(param -> param.equals("a"));

        // anyMatch:判断集合中的其中一个元素满足条件,返回true
        boolean anyMatch =resultList.stream().anyMatch(param->param.equals("a"));

        // noneMatch:判断集合中的所有元素都不满足条件,返回true
        boolean noneMatch =resultList.stream().noneMatch(param->param.equals("a"));

        System.out.println("allMatch="+allMatch);
        System.out.println("anyMatch="+anyMatch);
        System.out.println("noneMatch="+noneMatch);

    }
}

执行结果:

allMatch=false
anyMatch=true
noneMatch=false

标签:anyMatch,stream,anyMath,noneMatch,resultList,param,allMatch
来源: https://blog.csdn.net/hello_world_9664/article/details/122507292