List分页工具类

首先,编写分页工具类

package listPaging;

import java.util.*;

/**
 * @Auther: Sunny
 * @Date: 2018/8/20 0020 11:24
 * @Description:
 */
public class ListPagingUtil {

    /**
     * @param list 进行分页的list
     * @param pageNo 页码
     * @param pageSize 每页显示条数
     * @return 分页后数据
     */
    public static <T> List<T> listPaging(List<T> list, Integer pageNo, Integer pageSize){
        if(list == null){
            list = new ArrayList<T>();
        }
        if(pageNo == null){
            pageNo = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }
        if(pageNo <= 0){
            pageNo = 1;
        }

        int totalitems = list.size();
        List<T> pagingList = new ArrayList<T>();
        
        int totalNum = ((pageNo - 1) * pageSize) + pageSize > totalitems ? totalitems : ((pageNo - 1) * pageSize) + pageSize;
        for(int i = (pageNo-1)*pageSize; i < totalNum; i++) {
            pagingList.add(list.get(i));
        }
        return pagingList;
    }
}

然后,新建实体类Student

package listPaging;

/**
 * @Auther: Sunny
 * @Date: 2018/8/20 0020 11:28
 * @Description:
 */
public class Student {
    public Student() {
    }

    public Student(String id, String name, Integer age, String school) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.school = school;
    }

    private String id;
    private String name;
    private Integer age;
    private String school;

    public String getId() {
        return id;
    }

    public void setId(String 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 getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

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

最后,进行测试

package listPaging;

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

/**
 * @Auther: Sunny
 * @Date: 2018/8/20 0020 11:28
 * @Description:
 */
public class TestList {

    public static void main(String[] args) {
        Student s11 = new Student("1","张三1",18,"Java学院");
        Student s12 = new Student("1","张三2",18,"Java学院");
        Student s13 = new Student("1","张三3",18,"Java学院");
        Student s14 = new Student("1","张三4",18,"Java学院");
        Student s15 = new Student("1","张三5",18,"Java学院");
        Student s21 = new Student("2","李四1",18,"Android学院");
        Student s22 = new Student("2","李四2",18,"Android学院");
        Student s23 = new Student("2","李四3",18,"Android学院");
        Student s24 = new Student("2","李四4",18,"Android学院");
        Student s25 = new Student("2","李四5",18,"Android学院");
        Student s31 = new Student("3","王五1",18,"IOS学院");
        Student s32 = new Student("3","王五2",18,"IOS学院");
        Student s33 = new Student("3","王五3",18,"IOS学院");
        Student s34 = new Student("3","王五4",18,"IOS学院");
        Student s35 = new Student("3","王五5",18,"IOS学院");
        Student s41 = new Student("4","田七1",18,"Web学院");
        Student s42 = new Student("4","田七2",18,"Web学院");
        Student s43 = new Student("4","田七3",18,"Web学院");
        Student s44 = new Student("4","田七4",18,"Web学院");
        Student s45 = new Student("4","田七5",18,"Web学院");
        Student s51 = new Student("5","赵八1",18,"Product学院");
        Student s52 = new Student("5","赵八2",18,"Product学院");
        Student s53 = new Student("5","赵八3",18,"Product学院");
        Student s54 = new Student("5","赵八4",18,"Product学院");
        Student s55 = new Student("5","赵八5",18,"Product学院");

        List list = new ArrayList();
        list.add(s11);
        list.add(s12);
        list.add(s13);
        list.add(s14);
        list.add(s15);
        list.add(s21);
        list.add(s22);
        list.add(s23);
        list.add(s24);
        list.add(s25);
        list.add(s31);
        list.add(s32);
        list.add(s33);
        list.add(s34);
        list.add(s35);
        list.add(s41);
        list.add(s42);
        list.add(s43);
        list.add(s44);
        list.add(s45);
        list.add(s51);
        list.add(s52);
        list.add(s53);
        list.add(s54);
        list.add(s55);

        List paging = ListPagingUtil.listPaging(list, 3, 10);
        System.out.println(paging);
    }
}

输出结果如下:

[Student{id='5', name='赵八1', age=18, school='Product学院'}, Student{id='5', name='赵八2', age=18, school='Product学院'}, Student{id='5', name='赵八3', age=18, school='Product学院'}, Student{id='5', name='赵八4', age=18, school='Product学院'}, Student{id='5', name='赵八5', age=18, school='Product学院'}]

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值