侧边栏壁纸
  • 累计撰写 38 篇文章
  • 累计创建 25 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Java之Lists.Partition使用与坑

michloas
2022-10-20 / 0 评论 / 0 点赞 / 327 阅读 / 1502 字 / 正在检测是否收录...

背景

项目中使用Lists.Partition批量处理数据,在循环处理的过程中会有remove操作,导致部分数据更新有问题。

原因

最后自己单元测试发现是List<List> resultPartition = Lists.partition(list, 100)之后再对list进行remove操作,resultPartition也会被remove。看了源码才发现它最终会调用 list.subList。subList执行结果是获取ArrayList的一部分,返回的是ArrayList的部分视图。对子集合的操作会反映到原集合, 对原集合的操作也会影响子集合。

源码

public static <T> List<List<T>> partition(List<T> list, int size) {
    checkNotNull(list);
    checkArgument(size > 0);
    return (list instanceof RandomAccess)
        ? new RandomAccessPartition<T>(list, size)
        : new Partition<T>(list, size);
  }

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

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

    @Override
    public List<T> get(int index) {
      checkElementIndex(index, size());
      int start = index * size;
      int end = Math.min(start + size, list.size());
      return list.subList(start, end);
    }

    @Override
    public int size() {
      return IntMath.divide(list.size(), size, RoundingMode.CEILING);
    }

    @Override
    public boolean isEmpty() {
      return list.isEmpty();
    }
  }
0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区