流收集器

前言

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转正,可以放心使用,使用流处理器更好操作流

相关推荐
wuminyu3 小时前
OpenJDK学习笔记:Native线程如何回调Java
java·linux·c语言·jvm·c++
骑着蜗牛撵大象3273 小时前
AgentScope 搭大脑、Spring AI 当手脚:企业智能体推理与执行分层落地
java·人工智能·spring·智能体·工具调用·agentscope·模型编排
程序员小杰@3 小时前
SpringBoot + SpringCloudAlibaba 日志 & 链路追踪 完整总结
java·spring boot·后端
EatFan4 小时前
2026 后端 AI 集成选型拆解:Spring AI 2.0、MCP 协议与 Agent 内嵌三条路径的决策要点
java·人工智能·spring
PFinal社区_南丞4 小时前
别再手写 Ubuntu 配置了!我用 AI 搓了个免费纯前端服务器配置生成器:ubuntu.pub
后端
天远API5 小时前
零信任架构实战:基于天远运营商近3个月欠费次数构建自动化授信调额合规网关
java·人工智能·架构·自动化
SummerJokes5 小时前
Java 集合 List 全套实验|从基础增删改查到实体类存储
java·python·list
想不到一个好的ID5 小时前
G1 垃圾回收器
后端
ZhengEnCi6 小时前
模型配置页怎么做:多模型备用链自动切换,API Key 加密存储只显示掩码
后端·llm·ai编程
长不大的杨瑞杰6 小时前
Spring AI 企业级 RAG 实战:检索、重排、结构化输出与提示注入防护
java·人工智能·spring·llm·rag·springai