当前位置:   article > 正文

利用List.subList()方法对list集合进行分页_list sublist 分页

list sublist 分页
package com;

import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

public class ListUtils {

    public static <T> List<T> getList(List<T> list, int pageIndex, int pageSize){
        List<T> lists = new ArrayList<>();
        if(ObjectUtils.isNotEmpty(list)){
            int startIndex;
            int endIndex;
            int total = list.size();
            int num = total%pageSize;
            startIndex = (pageIndex - 1)*pageSize;
            if(startIndex > total){
                return new ArrayList<>();
            }
            endIndex = pageIndex * pageSize;
            if(endIndex > total) {
                endIndex = (pageIndex - 1)*pageSize + num;
            }
            lists = list.subList(startIndex, endIndex);
        }
        return lists;
    }
}

测试下

import com.ListUtils;
import com.google.common.collect.Lists;
import java.util.List;

public class HelloWord {

    public static void main(String[] args) {
        List<Integer> list = Lists.newArrayList(1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9);
        List<Integer> lists = ListUtils.getList(list, 3, 10);
        System.out.println(lists);
    }
}

控制台打印

[3, 4, 5, 6, 7, 8, 9]
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/1022294
推荐阅读
相关标签
  

闽ICP备14008679号