前提入参必须要有全部数据的list,pageNum为当前页数,pageSize为需要分多少页
public static <T> List<T> pageByJava(Collection<T> collection, Integer pageNo, Integer pageSize) {
if (collection == null || collection.isEmpty()) {
return new ArrayList<>();
}
// 开始下标
int startIndex = (pageNo - 1) * pageSize;
if (startIndex > collection.size() - 1) {
return new ArrayList<>();
}
// 结束下标
int entIndex = startIndex + pageSize - 1;
if (entIndex > collection.size() - 1) {
entIndex = collection.size() - 1;
}
List<T> result = new ArrayList<>(pageSize);
int currentIndex = 0;
for (T item : collection) {
if (currentIndex >= startIndex) {
result.add(item);
}
currentIndex += 1;
if (currentIndex > entIndex) {
break;
}
}
return result;
}