java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java List集合手动分页

Java实现List集合手动分页的方法

作者:师小师

在工作中难免会遇到,将组装的集合数据进行分页处理,本文主要介绍了Java实现List集合手动分页的方法,具有一定的参考价值,感兴趣的可以了解一下

为方便测试,可以直接在 controller 内添加一个方法,或者直接通过 main 方法测试

List 手动分页

@GetMapping("/getUserInfo")
    public Map<String,Object> getUserInfo(@RequestParam("pageNo") Integer pageNo,@RequestParam("pageSize") Integer pageSize){
        if(null == pageNo){
            pageNo = 1;
        }
        if(null == pageSize){
            pageSize = 10;
        }
        List<User> userList = new ArrayList<>();
        userList.add(new User(1,"奥迪1",23,"123@qq.com","地址1"));
        userList.add(new User(2,"奥迪2",23,"124@qq.com","地址2"));
        userList.add(new User(3,"奥迪3",23,"124@qq.com","地址3"));
        userList.add(new User(4,"奥迪4",23,"125@qq.com","地址4"));
        userList.add(new User(5,"奥迪4",23,"126@qq.com","地址5"));
        userList.add(new User(6,"奥迪5",23,"127@qq.com","地址6"));
        userList.add(new User(7,"奥迪6",23,"128@qq.com","地址7"));
        userList.add(new User(8,"奥迪7",23,"129@qq.com","地址8"));
        userList.add(new User(9,"奥迪7",23,"1219@qq.com","地址9"));
        userList.add(new User(10,"奥迪10",23,"1231@qq.com","地址10"));
        userList.add(new User(11,"奥迪11",23,"1232@qq.com","地址11"));

        Integer limit = (pageNo-1)*pageSize;
        Integer totalSize = userList.size();
        // 方法一
        Integer totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1;
        // 方法二(推荐)其中 pageSize-1 就是 totalSize/pageSize 的最大的余数
        // Integer totalPage = (totalSize + pageSize -1) / pageSize;
        // 方法三
        // Integer totalPage = (int) Math.ceil(totalSize / pageSize);

        if(pageNo > totalPage){
            throw new RuntimeException("页数超出了");
        }
        List<User> subList = null;
        if(pageNo == totalPage){
            // 最后一页
            subList = userList.subList(limit, totalSize);
        }else{
            Integer end= limit + pageSize;
            // 截取的最后的下标
            subList = userList.subList(limit, end);
            // 出现空值处理
            // subList = CollectionUtils.isEmpty(userList) ? new ArrayList<>() : userList.subList(limit, Math.min(userList.size(), end));
        }
        Map<String,Object> map = new HashMap<>();
        map.put("data",subList);
        map.put("total",userList.size());
        subList.forEach(System.out::println);
        return map;
    }
    static class User{
        private Integer id;
        private String name;
        private Integer age;
        private String email;
        private String address;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public User(Integer id, String name, Integer age, String email, String address) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.email = email;
            this.address = address;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", email='" + email + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    }

计算总页数方法

// totalPage 总页数,pageSize 每页显示多少条,totalSize 总条数
// 方法一
Integer totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1;
// 方法二(推荐)其中 pageSize-1 就是 totalSize/pageSize 的最大的余数
// Integer totalPage = (totalSize + pageSize -1) / pageSize;
// 方法三
// Integer totalPage = (int) Math.ceil(totalSize / pageSize);

补:

利用 Lists.partition() 方法进行数据将数据切割分页

List<UserEntity> resultList = new ArrayList<>();     // 将数据按照传过来页长进行切割
        List<List<UserEntity>> partition = Lists.partition(userList,pageSize);
        for (int i = 0; i < partition.size(); i++) {
            if (i == pageIndex){
                resultList  = partition.get(pageIndex);
            }
      }

利用封装的分页工具进行分页

(1)分页工具

/**
 * @project 
 * @Description 对List集合进行份分页
 * @Author songwp
 * @Date 2023/5/12 13:55
 **/
public class ListPagingUtil {
        private Integer currentPage;//当前页
        private Integer pageSize;//每页显示记录条数
        private Integer totalPage;//总页数
        private Integer star;//开始数据
        private Integer total;//总条数
        private List<?> dataList;//每页显示的数据

        public Integer getCurrentPage() {
            return currentPage;
        }

        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }

        public Integer getPageSize() {
            return pageSize;
        }

        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }

        public Integer getTotalPage() {
            return totalPage;
        }

        public void setTotalPage(Integer totalPage) {
            this.totalPage = totalPage;
        }

        public List<?> getDataList() {
            return dataList;
        }

        public void setDataList(List<?> dataList) {
            this.dataList = dataList;
        }

        public Integer getStar() {
            return star;
        }

        public void setStar(Integer star) {
            this.star = star;
        }

        public Integer getTotal() {
            return total;
        }

        public void setTotal(Integer total) {
            this.total = total;
        }

        @Override
        public String toString() {
            return "ListPagingUtil{" +
                    "currentPage=" + currentPage +
                    ", pageSize=" + pageSize +
                    ", totalPage=" + totalPage +
                    ", dataList=" + dataList +
                    ", star=" + star +
                    ", total=" + total +
                    '}';
        }

        public void pageStartInfo(Integer currentPage, Integer pageSize){
            //如果传入的pageNumber为null给pageNumber赋为1
            currentPage = currentPage == null ? 1 : currentPage;
            //如果传入的pageSize为null给pageSize赋为10
            pageSize = pageSize == null ? 10 : pageSize;
            this.setCurrentPage(currentPage);
            this.setPageSize(pageSize);
        }

        public static ListPagingUtil paging(Integer currentPage, Integer pageSize, List<?> list) {
            ListPagingUtil pagingUtil = new ListPagingUtil();
            //初始化
            pagingUtil.pageStartInfo(currentPage, pageSize);
            //设置起始数据
            pagingUtil.setStar((pagingUtil.getCurrentPage()-1)*pagingUtil.getPageSize());
            //设置总数
            pagingUtil.setTotal(list.size());
            //设置总页数
            pagingUtil.setTotalPage(pagingUtil.getTotal() % pagingUtil.getPageSize() == 0 ? pagingUtil.getTotal()/pagingUtil.getPageSize() :pagingUtil.getTotal()/pagingUtil.getPageSize()+1);
            //截取list
            pagingUtil.setDataList(list.subList(pagingUtil.getStar(), pagingUtil.getTotal()-pagingUtil.getStar()>pagingUtil.getPageSize()?pagingUtil.getStar()+pagingUtil.getPageSize():pagingUtil.getTotal()));
            return pagingUtil;
        }
}

(2)方法调用

ListPagingUtil.paging(pageIndex,pageSize, userList)

利用 stream进行分页 处理

方法一:

List<List<CompanyStaffEquipStatisticVO>> partition = ListUtils.partition(result, param.getPageSize());
        List<CompanyStaffEquipStatisticVO> companyStaffEquipStatisticVOS = param.getPageIndex() > partition.size() ? new ArrayList<>() : partition.get(param.getPageIndex() - 1);
        SafePageParam safePageParam = new SafePageParam();
        safePageParam.setPage(true);
        safePageParam.setPageSize(param.getPageSize());
        PageVO<CompanyStaffEquipStatisticVO> voResult = new PageVO<>(safePageParam, companyStaffEquipStatisticVOS, result.size());
        if (param.getPageIndex()*param.getPageSize()<result.size()) {
            voResult.setHasNext(true);
        }
        return voResult;

方法二:

List<VendorExcelVO> resultVO = getVendorProjectListInfo(param);
PageVO<VendorExcelVO> voPageVO = new PageVO<>();
List<VendorExcelVO> collectList = resultVO.stream().skip((param.getPageIndex() - 1) * param.getPageSize()).limit(param.getPageSize()).collect(Collectors.toList());
voPageVO.setPage(true);
voPageVO.setPageSize(param.getPageSize());
voPageVO.setCount(resultVO.size());
voPageVO.setList(collectList);
if (param.getPageIndex() * param.getPageSize() < resultVO.size()){
    voPageVO.setHasNext(true);
}
return voPageVO;

 到此这篇关于Java实现List集合手动分页的方法的文章就介绍到这了,更多相关Java List集合手动分页内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文