java8 stream接口终端操作 count,anyMatch,allMatch,noneMatch
函数定义:
long count(); boolean anyMatch(Predicate<? super T> predicate); boolean allMatch(Predicate<? super T> predicate); boolean noneMatch(Predicate<? super T> predicate);
anyMatch表示,判断的条件里,任意一个元素成功,返回true
allMatch表示,判断条件里的元素,所有的都是,返回true
noneMatch跟allMatch相反,判断条件里的元素,所有的都不是,返回true
例子:
List<String> strs = Arrays.asList("a", "a", "a", "a", "b"); boolean aa = strs.stream().anyMatch(str -> str.equals("a")); boolean bb = strs.stream().allMatch(str -> str.equals("a")); boolean cc = strs.stream().noneMatch(str -> str.equals("a")); long count = strs.stream().filter(str -> str.equals("a")).count(); System.out.println(aa);// TRUE System.out.println(bb);// FALSE System.out.println(cc);// FALSE System.out.println(count);// 4
参考: https://blog.csdn.net/qq_28410283/article/details/80783946