ListUtils.java
1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.ruoyi.utils;
import cn.hutool.core.collection.CollectionUtil;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* 集合工具类
* @author 20412
*/
public class ListUtils<T>{
public static <T> List<List<T>> splitList(List<T> list, int batchSize) {
List<List<T>> batchList = new ArrayList<>();
for (int i = 0; i < list.size(); i += batchSize) {
int endIndex = Math.min(i + batchSize, list.size());
List<T> subList = list.subList(i, endIndex);
batchList.add(subList);
}
return batchList;
}
/**
* 深度备份
* @param sourceList
* @return
* @param <T>
* @throws IOException
* @throws ClassNotFoundException
*/
public static <T> List<T> deepCopy(List<T> sourceList) throws IOException, ClassNotFoundException{
if (CollectionUtil.isEmpty(sourceList)){
return new ArrayList<>();
}
ByteArrayOutputStream bo= new ByteArrayOutputStream();
ObjectOutputStream oos= new ObjectOutputStream(bo);
oos.writeObject(sourceList);
ByteArrayInputStream bi= new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream ois=new ObjectInputStream(bi);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>)ois.readObject();
return dest;
}
}