文章目录
- 前言
- 实现代码
- 执行结果
 
 
 
前言
在项目中有时会出现列表很大,无法一次性批量操作,我们需要将列表分成指定大小的几个子列表,一份一份进行操作,本文提供这样的工具类实现这个需求。
实现代码
以下为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>> {
        private final List<T> list;
        private final int size;
       /**
        * 
        * @param list 传入的列表
        * @param size 指定的长度,每size个数据切割为一个子列表
        */
        private Partition(final List<T> list, final int size) {
            this.list = list;
            this.size = size;
        }
       /**
        * 获取按指定长度拆分后,索引位置的子列表
        * @param index
        * @return
        */
        @Override
        public List<T> get(final int index) {
            //获取拆分后子列表的个数
            final int listSize = size();
            if (index < 0) {
                throw new IndexOutOfBoundsException("Index " + index + " must not be negative");
            }
            if (index >= listSize) {
                throw new IndexOutOfBoundsException("Index " + index + " must be less than size " +
                        listSize);
            }
            final int start = index * size;
            final int end = Math.min(start + size, list.size());
            //返回子列表
            return list.subList(start, end);
        }
        @Override
        public int size() {
            //(传入列表总长度/指定的长度)向上取整,即为拆分后子列表的个数
            return (int) Math.ceil((double) list.size() / (double) size);
        }
        @Override
        public boolean isEmpty() {
            return list.isEmpty();
        }
    }
}
执行结果
-  在上述类里写个main方法用以测试结果。 public static void main(String[] args) { List<String> list=new ArrayList<String>(); for (int i = 0; i <= 2000; i++) { list.add(i+""); } //将list每2000条数据拆分成一个子列表 List<List<String>> partition = ListUtils.partition(list, 2000); System.out.println("将list每2000条数据拆分成一个子列表:"); System.out.println("子列表个数:"+partition.size()); System.out.println("第二个子列表的内容:"); partition.get(1).forEach(System.out::print); System.out.println("-------------------------------------------------------"); System.out.println("将list每10条数据拆分成一个子列表:"); List<List<String>> partition1 = ListUtils.partition(list, 10); System.out.println("子列表个数:"+partition1.size()); System.out.println("第三个子列表的内容:"); partition1.get(2).forEach(s -> { System.out.print(s+" "); }); }
-  执行main方法,得到结果如下: 
  
-  分析结果 将list每2000条数据拆分成一个子列表后,子列表个数为2,第一个子列表里的内容{0,1,…,1998,1999},第二个子列表的内容为{2000}。 
 将list每10条数据拆分成一个子列表后,子列表个数为201,分别为{0,1,…,8,9},……,{1990,1991,…,1998,1999},{2000}。
 结果符合我们的要求,通过这个工具类,我们实现了所需功能。


![[HTML]Web前端开发技术12(HTML5、CSS3、JavaScript )——喵喵画网页](https://img-blog.csdnimg.cn/direct/3459348837da4004922ca6eba73cbfcf.png)
















