List 集合手动分页

小伙伴们好,欢迎关注,一起学习,无限进步

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

List 手动分页:

java 复制代码
@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,"[email protected]","地址1"));
        userList.add(new User(2,"奥迪2",23,"[email protected]","地址2"));
        userList.add(new User(3,"奥迪3",23,"[email protected]","地址3"));
        userList.add(new User(4,"奥迪4",23,"[email protected]","地址4"));
        userList.add(new User(5,"奥迪4",23,"[email protected]","地址5"));
        userList.add(new User(6,"奥迪5",23,"[email protected]","地址6"));
        userList.add(new User(7,"奥迪6",23,"[email protected]","地址7"));
        userList.add(new User(8,"奥迪7",23,"[email protected]","地址8"));
        userList.add(new User(9,"奥迪7",23,"[email protected]","地址9"));
        userList.add(new User(10,"奥迪10",23,"[email protected]","地址10"));
        userList.add(new User(11,"奥迪11",23,"[email protected]","地址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 + '\'' +
                    '}';
        }
    }

计算总页数方法

java 复制代码
// 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);
相关推荐
只可远观2 小时前
Flutter Dart 集合类型List Set Map详解军 以及循环语句 forEaclh map where any every
windows·flutter·list
几点才到啊3 小时前
使用 malloc 函数模拟开辟一个 3x5 的整型二维数组
数据结构·算法
<但凡.6 小时前
C++修炼:list模拟实现
开发语言·数据结构·c++
敲代码的瓦龙6 小时前
C++?动态内存管理!!!
c语言·开发语言·数据结构·c++·后端
Ronin3056 小时前
【C++】13.list的模拟实现
开发语言·数据结构·c++·list
序属秋秋秋7 小时前
《数据结构初阶》【顺序表 + 单链表 + 双向链表】
c语言·数据结构·笔记·链表
无敌的牛8 小时前
AVL树的介绍与学习
数据结构·学习
想不明白的过度思考者8 小时前
初识数据结构——二叉树从基础概念到实践应用
数据结构·二叉树
Brookty10 小时前
【数据结构】哈希表
数据结构·算法·哈希算法·散列表
Dovis(誓平步青云)11 小时前
【数据结构】·励志大厂版(复习+刷题):二叉树
c语言·数据结构·经验分享·笔记·学习·算法·学习方法