搜索
简帛阁>技术文章>JAVA List集合转Page(分页对象)

JAVA List集合转Page(分页对象)

/**
 * @version 1.0
 * @author: fwjia
 */
import java.util.List;

public class PageModel<T> {
    /***
     * 当前页
     */
    private int page = 1;

    /***
     * 总页数
     */
    public int totalPages = 0;

    /***
     * 每页数据条数
     */
    private int pageRecorders;

    /***
     * 总页数
     */
    private int totalRows = 0;

    /***
     * 每页的起始数
     */
    private int pageStartRow = 0;

    /***
     * 每页显示数据的终止数
     */
    private int pageEndRow = 0;

    /***
     * 是否有下一页
     */
    private boolean hasNextPage = false;

    /***
     * 是否有前一页
     */
    private boolean hasPreviousPage = false;

    /***
     * 数据集合
     */
    private List<T> list;


    public PageModel(List<T> list, int pageRecorders) {
        // 通过对象集,记录总数划分
        init(list, pageRecorders);
    }

    /** *//**
     * 初始化list,并告之该list每页的记录数
     * @param list 数据几个
     * @param pageRecorders 一页显示多少数据
     */
    public void init(List<T> list, int pageRecorders) {
        this.pageRecorders = pageRecorders;
        this.list = list;
        totalRows = list.size();
        hasPreviousPage = false;
        if ((totalRows % pageRecorders) == 0) {
            totalPages = totalRows / pageRecorders;
        } else {
            totalPages = totalRows / pageRecorders + 1;
        }

        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }

        if (totalRows < pageRecorders) {
            this.pageStartRow = 0;
            this.pageEndRow = totalRows;
        } else {
            this.pageStartRow = 0;
            this.pageEndRow = pageRecorders;
        }
    }


    // 判断要不要分页
    public boolean isNext() {
        return list.size() > 5;
    }

    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }

    public String toString(int temp) {
        String str = Integer.toString(temp);
        return str;
    }

    public void description() {

        String description = "共有数据数:" + this.getTotalRows() +

                "共有页数: " + this.getTotalPages() +

                "当前页数为:" + this.getPage() +

                " 是否有前一页: " + this.isHasPreviousPage() +

                " 是否有下一页:" + this.isHasNextPage() +

                " 开始行数:" + this.getPageStartRow() +

                " 终止行数:" + this.getPageEndRow();

        System.out.println(description);
    }

    public List getNextPage() {
        page = page + 1;

        disposePage();

        System.out.println("用户凋用的是第" + page + "页");
        this.description();
        return getObjects(page);
    }

    /**
     * 处理分页
     */
    private void disposePage() {

        if (page == 0) {
            page = 1;
        }

        if ((page - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }

        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
    }

    public List getPreviousPage() {

        page = page - 1;

        if ((page - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }
        if (page >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
        this.description();
        return getObjects(page);
    }

    /**
     * 获取第几页的内容
     *
     * @param page 当前页面
     * @return
     */
    public List<T> getObjects(int page) {
        if(page == 0){
            this.setPage(1);
        }
        else{
            this.setPage(page);
        }
        this.disposePage();
        if (page * pageRecorders < totalRows) {
            // 判断是否为最后一页
            pageEndRow = page * pageRecorders;
            pageStartRow = pageEndRow - pageRecorders;
        } else {
            pageEndRow = totalRows;
            pageStartRow = pageRecorders * (totalPages - 1);
        }

        List<T> objects = null;
        if (!list.isEmpty()) {
            objects = list.subList(pageStartRow, pageEndRow);
        }
        //this.description();
        return objects;
    }

    public List<T> getFistPage() {
        if (this.isNext()) {
            return list.subList(0, pageRecorders);
        } else {
            return list;
        }
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }


    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }


    public List getList() {
        return list;
    }


    public void setList(List list) {
        this.list = list;
    }


    public int getPage() {
        return page;
    }


    public void setPage(int page) {
        this.page = page;
    }


    public int getPageEndRow() {
        return pageEndRow;
    }


    public void setPageEndRow(int pageEndRow) {
        this.pageEndRow = pageEndRow;
    }


    public int getPageRecorders() {
        return pageRecorders;
    }


    public void setPageRecorders(int pageRecorders) {
        this.pageRecorders = pageRecorders;
    }


    public int getPageStartRow() {
        return pageStartRow;
    }


    public void setPageStartRow(int pageStartRow) {
        this.pageStartRow = pageStartRow;
    }


    public int getTotalPages() {
        return totalPages;
    }


    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }


    public int getTotalRows() {
        return totalRows;
    }


    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }


    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }

}

  测试类TestPagerModel 

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

/**
 * @version 1.0
 * @author: fwjia
 */
public class TestPagerModel {

    public static void main(String args[]) {
        List list = new ArrayList();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");
        list.add("f");
        list.add("g");
        list.add("h");
        list.add("h");
        list.add("i");
        list.add("j");
        list.add("k");
        list.add("l");
        list.add("m");
        PageModel pm = new PageModel(list, 5);

        List sublist = pm.getObjects(2);
        for(int i = 0; i < sublist.size(); i++) {
            System.out.println(sublist.get(i));
        }
    }

}

 

转载于:https://www.cnblogs.com/jiafuwei/p/10096183.html

/***@version10*@author:fwjia*/importjavautilList;publicclassPageModel<T>{/****当前页*/privateintp
ListList是一个有序的集合,可以包含重复的元素List的两种遍历方式迭代器对象遍历//获取当前list对象的迭代器对象Iteratoriteratorlistiterator();//遍历集合
原文链接:https://wwwcnblogscom/haiyangsvs/p/6210852htmlimportjavautilArrays;importjavautilCollections;im
需求:班级与教师是多对多关系,在后台班级管理需要添加一个接口,传入教师的id和pageable,返回带分页数据的班级信息。Page<Klass>pageByTeacher(Longteac
publicstaticvoidmain(String[]args){ArrayListarrayListnewArrayList();arrayListadd(啊哈哈);arrayListadd(反
对象POJO和JSON互转publicclassJsonUtil{/***JSONPOJO*/publicstatic<T>TgetObject(Stringpojo,Class<
一、List集合11List集合概述和特点111、List集合概述有序集合(也成为序列:用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素,并搜索列表中的元素。与Set集合不同
内容简介本文主要说明在Java8及以上版本中,使用stream()filter()来过滤一个List对象,查找符合条件的对象集合List对象类(StudentInfopublicclassStud
importjavautilArrayList;importjavautilList;importjavautilMap;importjavautilstreamCollectors;publiccl
List集合的子类一、ArrayList集合(实现类1、特点1、底层数据结构是:数组(元素增删慢,查找快;2、此实现不是同步的(多线程、线程不安全;3、ArrayList使用无参构造函数创建对象