List拆分成相同长度的子List

本文讲述了如何通过使用Apache Commons Collections的ListUtils.partition方法,将50条数据分批处理,以解决因一次性处理过多导致的connectionholderisnull问题。通过将每批次处理的记录限制为10条,有效地解决了接口调用响应延迟问题。
摘要由CSDN通过智能技术生成

关于List太大需要处理的时间过长导致connection holder is null 这件事

关键字:list等长拆分、list
事情是这样子的,在有一次接口调用,请求的数据有50条,处理的时间太长,导致报了connection holder is null的错误
所以就先将每次处理的条数改成10条,

import org.apache.commons.collections4.ListUtils;
method(){
 List<CaseInsensitiveMap> rowList = Arrays.stream(rows).collect(Collectors.toList());
 List<List<CaseInsensitiveMap>>  sublist = ListUtils.partition(rowList,10);
}

没错就是这个 ListUtils.partition(List list, int size)方法,太好用了,上源码

    private static class Partition<T> extends AbstractList<List<T>> {
        private final List<T> list;
        private final int size;

        private Partition(List<T> list, int size) {
            this.list = list;
            this.size = size;
        }

        public List<T> get(int index) {
            int listSize = this.size();
            if (listSize < 0) {
                throw new IllegalArgumentException("negative size: " + listSize);
            } else if (index < 0) {
                throw new IndexOutOfBoundsException("Index " + index + " must not be negative");
            } else if (index >= listSize) {
                throw new IndexOutOfBoundsException("Index " + index + " must be less than size " + listSize);
            } else {
                int start = index * this.size;
                int end = Math.min(start + this.size, this.list.size());
                return this.list.subList(start, end);
            }
        }

        public int size() {
            return (this.list.size() + this.size - 1) / this.size;
        }

        public boolean isEmpty() {
            return this.list.isEmpty();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值