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<String> 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<String> list, int subSize) {
        long beginTime = System.currentTimeMillis();
        if (StringUtils.isEmpty(list)) {
            return;
        }

        int beginIndex = 0;
        List<String> 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<String> list, int subSize) {
        long beginTime = System.currentTimeMillis();
        if (StringUtils.isEmpty(list)) {
            return;
        }

        int beginIndex = 0;
        List<String> 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));
    }


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

        List<List<String>> 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<String> list, int subSize) {
        long beginTime = System.currentTimeMillis();
        List<String> 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));
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值