Java將列表直接分頁

import com.baomidou.mybatisplus.core.metadata.IPage;
public class PageUtils implements Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * 總記錄數
	 */
	private int totalCount;
	/**
	 * 每頁記錄數
	 */
	private int pageSize;
	/**
	 * 總頁數
	 */
	private int totalPage;
	/**
	 * 當前頁數
	 */
	private int currPage;
	/**
	 * 列表數據
	 */
	private List<?> list;
	
	/**
	 * 分頁
	 * @param list        列表數據
	 * @param totalCount  總記錄數
	 * @param pageSize    每頁記錄數
	 * @param currPage    當前頁數
	 */
	public PageUtils(List<?> list, int totalCount, int pageSize, int currPage) {
		this.list = list;
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		this.currPage = currPage;
		this.totalPage = (int)Math.ceil((double)totalCount/pageSize);
	}

	/**
	 * 分頁
	 */
	public PageUtils(IPage<?> page) {
		this.list = page.getRecords();
		this.totalCount = (int)page.getTotal();
		this.pageSize = (int)page.getSize();
		this.currPage = (int)page.getCurrent();
		this.totalPage = (int)page.getPages();
	}

.....

// 列表直接分頁
	public  static  List<Object> pageBySubList(List list, int pagesize, int currentPage) {
		int totalcount = list.size();
		int pagecount = 0;
		List<Object>  subList=new ArrayList<>();
		int m = totalcount % pagesize;
		if (m > 0) {
			pagecount = totalcount / pagesize + 1;
		} else {
			pagecount = totalcount / pagesize;
		}
		if(((currentPage - 1) * pagesize )>=totalcount){
			return subList;
		}
		if (m == 0) {
			subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage)>totalcount?totalcount-1:pagesize * (currentPage));
		} else {
			if (currentPage == pagecount) {
				subList = list.subList((currentPage - 1) * pagesize, totalcount);
			} else {
				subList = list.subList((currentPage - 1) * pagesize, pagesize * (currentPage)>totalcount?totalcount-1:pagesize * (currentPage));
			}
		}
		return subList;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章