对于list集合进行分页处理
package com.octv.cloud.interact.utils;
import cn.hutool.core.util.ObjectUtil;
import java.util.ArrayList;
import java.util.List;
public class PageUtil {
public static<T> List<T> startPage(List<T> list, Integer pageNum, Integer pageSize) {
if (ObjectUtil.isEmpty(list)) {
return list;
}
if (pageSize == 0 || pageNum == 0) {
return new ArrayList<T>();
}
Integer count = list.size();
int pageCount = 0;
if (count % pageSize == 0) {
pageCount = count / pageSize;
} else {
pageCount = count / pageSize + 1;
}
int fromIndex = 0;
int toIndex = 0;
fromIndex = (pageNum - 1) * pageSize > count ? Math.max(count - pageSize, 0) : (pageNum - 1) * pageSize;
if (pageNum != pageCount) {
toIndex = fromIndex + pageSize > count ? count : fromIndex + pageSize;
} else {
toIndex = count;
}
return list.subList(fromIndex, toIndex);
}
}