流收集器

前言

JDK22引入了流收集器,也就是Gatherers关键字,该东西经过几次预览之后,在JDK24转正

流收集器使用

windowFixed方使用

csharp 复制代码
public class GathererDemo {

    static void main() {
        List<List<Integer>> fixedWindows = IntStream.range(0, 10).boxed()
                .gather(Gatherers.windowFixed(3))
                .toList();
        System.out.println( fixedWindows);
    }
}

输出结果为

windowSliding使用

csharp 复制代码
public class Gatherer1Demo {

    public static void main() {
        List<List<Integer>> list = Stream.iterate(0, i -> i + 1)
                .gather(Gatherers.windowSliding(2))
                .limit(5)
                .collect(Collectors.toList());
        System.out.println(list);
    }
}

输出结果为

总结

该特性在JDK24转正,可以放心使用,使用流处理器更好操作流

相关推荐
关于不上作者榜就原神启动那件事14 小时前
从 MDC 到 Agent:我手搓的文档路由协议,在 Spring AI Alibaba 里找到了正式实现
java·人工智能·spring·ai·agent
用户693717500138414 小时前
了解一下 Agent Harness
android·前端·后端
swipe15 小时前
16|(前端转全栈)前端人排查后端问题:curl、traceId、日志、MySQL、Redis 怎么用?
前端·后端·面试
亦暖筑序15 小时前
重新认识 AgentScope-Java 2.0:ReActAgent 负责推理,HarnessAgent 负责运行
java·后端·agent
小林ixn15 小时前
Redis 实战避坑指南:从缓存击穿到高可用,一文全搞定
redis·后端
苍何15 小时前
用 AI 做了套品牌周边,感觉能挂Shopify开卖了
后端
木井巳15 小时前
【DFS解决floodfill算法】岛屿的最大面积
java·算法·leetcode·深度优先
用户2986985301415 小时前
免费在线将 PDF 转为 Word:5 款好用的转换工具
人工智能·后端
newerp15 小时前
unsafe 包与底层编程
后端
orient15 小时前
并发问题排查实战
后端