前言
在项目中有时会出现列表很大,无法一次性批量操作,我们需要将列表分成指定大小的几个子列表,一份一份进行操作,本文提供这样的工具类实现这个需求。
实现代码
以下为ListUtil工具类代码实现:
public class ListUtils {
public static <T> List<List<T>> partition(final List<T> list, final int size) {
if (list == null) {
throw new NullPointerException("List must not be null");
}
if (size <= 0) {
throw new IllegalArgumentException("Size must be greater than 0");
}
return new Partition<>(list, size);
}
private static class Partition<T> extends AbstractList<List<T>> {
<

文章介绍了如何使用自定义的ListUtil工具类,通过partition方法将大列表按指定长度分割成多个子列表,提供示例展示了每2000和每10条数据的分割效果。
最低0.47元/天 解锁文章
832

被折叠的 条评论
为什么被折叠?



