首页 开发教程 流收集器

流收集器

开发教程 2025年12月4日
261 浏览

前言

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

收集器使用

windowFixed方使用

public class GathererDemo {

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

输出结果为

image.png

windowSliding使用

public class Gatherer1Demo {

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

输出结果为

image.png

总结

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

发表评论
暂无评论

还没有评论呢,快来抢沙发~

客服

点击联系客服 点击联系客服

在线时间:09:00-18:00

关注微信公众号

关注微信公众号
客服电话

400-888-8888

客服邮箱 122325244@qq.com

手机

扫描二维码

手机访问本站

扫描二维码
搜索