list分段截取方法

 对list 分段截取方法是一个常见的操作,通常用于对list数据批量操作,常见的场景有返回分页展示数据,对大数据进行分批次插入数据库等

package com.hmdp.dto;

import org.apache.commons.collections4.ListUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: ldj
 * @Date: 2023/06/19/16:34
 * @Description: 对list分段截取
 */
public class Tr {
    public static void main(String[] args) throws ParseException {
        List list = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i");
        Tr.subList1(list, 5);
        Tr.subList2(list, 5);
        Tr.subList3(list, 5);
        Tr.subList4(list, 5);
    }

    //方法1
    public static void subList1(List list, int subSize) {
        long beginTime = System.currentTimeMillis();
        if (StringUtils.isEmpty(list)) {
            return;
        }

        int beginIndex = 0;
        List arrayList = null;

        while (beginIndex < list.size()) {
            if (beginIndex + subSize < list.size()) {
                arrayList = list.subList(beginIndex, beginIndex + subSize);
            } else {
                arrayList = list.subList(beginIndex, list.size());
            }
            beginIndex += subSize;
            System.out.println(arrayList);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("subList1 cost time:" + (endTime - beginTime));
    }

    //方法2快一丢丢
    public static void subList2(List list, int subSize) {
        long beginTime = System.currentTimeMillis();
        if (StringUtils.isEmpty(list)) {
            return;
        }

        int beginIndex = 0;
        List arrayList = null;

        if (list.size() % subSize == 0) {
            int size = list.size() / subSize;
            for (int i = 0; i < size; i++) {
                arrayList = list.subList(beginIndex, beginIndex + subSize);
                beginIndex = beginIndex + subSize;
                System.out.println(arrayList);
            }
        }

        if (list.size() % subSize > 0) {
            int size = list.size() / subSize;
            for (int i = 0; i < size; i++) {
                arrayList = list.subList(beginIndex, beginIndex + subSize);
                beginIndex = beginIndex + subSize;
                System.out.println(arrayList);
            }
            arrayList = list.subList(beginIndex, list.size());
            System.out.println(arrayList);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("subList2 cost time:" + (endTime - beginTime));
    }


    /**
     * 
     * org.apache.commons
     * commons-collections4
     * 4.4
     * 
     */
    //方法3
    public static void subList3(List list, int subSize) {
        long beginTime = System.currentTimeMillis();

        List> partitions = ListUtils.partition(list, subSize);
        partitions.forEach(System.out::println);
        long endTime = System.currentTimeMillis();
        System.out.println("subList3 cost time:" + (endTime - beginTime));
    }

    //方法4(推荐)
    public static void subList4(List list, int subSize) {
        long beginTime = System.currentTimeMillis();
        List arrayList = null;
        for (int beginIndex = 0; beginIndex < list.size(); beginIndex += subSize) {
            int endIndex = Math.min(beginIndex + subSize, list.size());
            arrayList = list.subList(beginIndex, endIndex);
            System.out.println(arrayList);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("subList4 cost time:" + (endTime - beginTime));
    }

}

 list分段截取方法_第1张图片

你可能感兴趣的:(subList,java)