
说明
1、count是终端操作,可以统计stream流中的元素总数,返回值为long类型。
2、count()返回流中元素的计数。这是归纳的特殊情况(归纳运算采用一系列输入元素,通过重复应用组合运算将其组合成一个总结结果)。这是终端操作,可能会产生结果和副作用。执行终端操作后,管道被视为消耗,无法再利用。
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | boolean anyStartsWithA =
stringCollection
.stream()
.anyMatch((s) -> s.startsWith( "a" ));
System.out.println(anyStartsWithA);
boolean allStartsWithA =
stringCollection
.stream()
.allMatch((s) -> s.startsWith( "a" ));
System.out.println(allStartsWithA);
boolean noneStartsWithZ =
stringCollection
.stream()
.noneMatch((s) -> s.startsWith( "z" ));
System.out.println(noneStartsWithZ);
|
以上就是java Count计算流中元素的方法,希望对大家有所帮助。更多Java学习指路:Java基础
推荐操作环境:windows7系统、java10版,DELL G3电脑。