/**
* 手动实现分页功能
*
* @param list 需要分页的集合
* @param pageNo 页码
* @param pageSize 每页大小
* @param <T> 集合内的泛型
* @return 分页后的集合
*/
private static <T> List<T> getAfterPageList(List<T> list, Integer pageNo, Integer pageSize) {
if (CollectionUtils.isEmpty(list)) {
return Collections.emptyList();
}
if (pageNo < 0 || pageSize < 0) {
throw new RuntimeException("Page number and page size must be greater than zero.");
}
int fromIndex = (pageNo - 1) * pageSize;
if (fromIndex >= list.size()) {
return Collections.emptyList();
}
int toIndex = Math.min(fromIndex + pageSize, list.size());
return list.subList(fromIndex, toIndex);
}
Java实现手动分页
于 2024-01-23 16:48:41 首次发布