import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
* 分页工具类
*
*/
public class PageUtil {
public static <T> List<T> getPageInfo(Integer pageNum, Integer pageSize, List<T> list) {
if (list == null) {
return null;
}
if (list.size() == 0) {
return null;
}
Integer count = list.size();
Integer pageCount = 0;
if (count % pageSize == 0) {
pageCount = count / pageSize;
} else {
pageCount = count / pageSize + 1;
}
int fromIndex = 0;
int toIndex = 0;
if (!pageNum.equals(pageCount)) {
fromIndex = (pageNum - 1) * pageSize;
toIndex = pageSize;
} else {
fromIndex = (pageNum - 1) * pageSize;
toIndex = pageSize;
}
return list.stream().skip(fromIndex).limit(toIndex).collect(Collectors.toList());
}
public static List getPage(Integer pageNum, Integer pageSize, List list) {
int fromIndex = (pageNum - 1) * pageSize;
int toIndex = fromIndex + pageSize;
if (pageNum >= 1 && list.size() >= toIndex) {
return (List) list.stream().skip(fromIndex).limit(toIndex).collect(Collectors.toList());
} else {
if (list.size() > toIndex - pageSize) {
fromIndex = (list.size() / pageSize) * pageSize;
return (List) list.stream().skip(fromIndex).limit(list.size()).collect(Collectors.toList());
} else {
return new ArrayList();
}
}
}
}
分页工具类
最新推荐文章于 2024-04-27 17:30:34 发布