Java教程

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

这篇关于Java8 stream-anyMath\allMatch\noneMatch总结的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!