关于List太大需要处理的时间过长导致connection holder is null 这件事
关键字:list等长拆分、list
事情是这样子的,在有一次接口调用,请求的数据有50条,处理的时间太长,导致报了connection holder is null的错误
所以就先将每次处理的条数改成10条,
import org.apache.commons.collections4.ListUtils;
method(){
List<CaseInsensitiveMap> rowList = Arrays.stream(rows).collect(Collectors.toList());
List<List<CaseInsensitiveMap>> sublist = ListUtils.partition(rowList,10);
}
没错就是这个 ListUtils.partition(List list, int size)方法,太好用了,上源码
private static class Partition<T> extends AbstractList<List<T>> {
private final List<T> list;
private final int size;
private Partition(List<T> list, int size) {
this.list = list;
this.size = size;
}
public List<T> get(int index) {
int listSize = this.size();
if (listSize < 0) {
throw new IllegalArgumentException("negative size: " + listSize);
} else if (index < 0) {
throw new IndexOutOfBoundsException("Index " + index + " must not be negative");
} else if (index >= listSize) {
throw new IndexOutOfBoundsException("Index " + index + " must be less than size " + listSize);
} else {
int start = index * this.size;
int end = Math.min(start + this.size, this.list.size());
return this.list.subList(start, end);
}
}
public int size() {
return (this.list.size() + this.size - 1) / this.size;
}
public boolean isEmpty() {
return this.list.isEmpty();
}
}