java Count怎么计算流中的元素

WBOY
WBOY转载
2023-05-11 16:07:06843浏览

说明

1、count是终端操作,可以统计stream流中的元素总数,返回值为long类型。

2、count()返回流中元素的计数。这是归纳的特殊情况(归纳运算采用一系列输入元素,通过重复应用组合运算将其组合成一个总结结果)。这是终端操作,可能会产生结果和副作用。执行终端操作后,管道被视为消耗,无法再利用。

实例

// 验证 list 中 string 是否有以 a 开头的, 匹配到第一个,即返回 true
boolean anyStartsWithA =
    stringCollection
        .stream()
        .anyMatch((s) -> s.startsWith("a"));
 
System.out.println(anyStartsWithA);      // true
 
// 验证 list 中 string 是否都是以 a 开头的
boolean allStartsWithA =
    stringCollection
        .stream()
        .allMatch((s) -> s.startsWith("a"));
 
System.out.println(allStartsWithA);      // false
 
// 验证 list 中 string 是否都不是以 z 开头的,
boolean noneStartsWithZ =
    stringCollection
        .stream()
        .noneMatch((s) -> s.startsWith("z"));
 
System.out.println(noneStartsWithZ);      // true

以上就是java Count怎么计算流中的元素的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:亿速云,如有侵犯,请联系admin@php.cn删除
PHP培训优惠套餐