对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);
Tr.subList2(list);
Tr.subList3(list);
Tr.subList4(list); //推荐
System.out.println(Math.min(3, 2));
}
public static void subList1(List<String> list) {
long beginTime = System.currentTimeMillis();
if (StringUtils.isEmpty(list)) {
return;
}
int beginIndex = 0;
int subSize = 2;
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) {
long beginTime = System.currentTimeMillis();
if (StringUtils.isEmpty(list)) {
return;
}
int beginIndex = 0;
int subSize = 2;
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>
*/
public static void subList3(List<String> list) {
long beginTime = System.currentTimeMillis();
List<List<String>> partitions = ListUtils.partition(list, 2);
partitions.forEach(partition -> {
System.out.println(partition);
});
long endTime = System.currentTimeMillis();
System.out.println("subList3 cost time:" + (endTime - beginTime));
}
public static void subList4(List<String> list) {
long beginTime = System.currentTimeMillis();
List<String> arrayList = null;
int subSize = 2;
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));
}
}