商城首页欢迎来到中国正版软件门户

您的位置:首页 >java Count怎么计算流中的元素

java Count怎么计算流中的元素

  发布于2023-04-27 阅读(0)

扫一扫,手机访问

说明

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
本文转载于:https://www.yisu.com/zixun/579206.html 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注