List数据手动分页

List数据手动分页

java中的list手动分页的代码,前提是list在手动分页之前必须排序!

list.sort(Comparator.comparing(SuperEntity::getId));

手动分页代码如下:

//创建Page类
Page page1 = new Page(page, limit);
//为Page类中的total属性赋值
int total = list.size();
page1.setTotal(total);
//计算数据下标起始值
int startIndex = (page - 1) * limit;
int endIndex = Math.min(startIndex + limit, total);
if (total > startIndex) {
    
    
    //截取数据
    page1.addAll(list.subList(startIndex, endIndex));
    //创建PageInfo
    PageInfo pageInfo = new PageInfo<>(page1);
    return Utils.toJSONString("获取成功", pageInfo.getList(), (int) pageInfo.getTotal());
} else {
    
    
    return Utils.toJSONString("获取成功", list, list.size());
}

猜你喜欢

转载自blog.csdn.net/weixin_44860226/article/details/131976615