JAVA List集合转Page(分页对象) java 分页 PageModel 测试类TestPagerModel
?
http://www.blogjava.net/jzone/archive/2015/06/05/308842.html?opt=admin
/**
?* @version 1.0
?* @author: fwjia
?*/
import?java.util.List;
?
public?class?PageModel<T> {
????/***
?????* 當(dāng)前頁(yè)
?????*/
????private?int?page =?1;
?
????/***
?????* 總頁(yè)數(shù)
?????*/
????public?int?totalPages =?0;
?
????/***
?????* 每頁(yè)數(shù)據(jù)條數(shù)
?????*/
????private?int?pageRecorders;
?
????/***
?????* 總頁(yè)數(shù)
?????*/
????private?int?totalRows =?0;
?
????/***
?????* 每頁(yè)的起始數(shù)
?????*/
????private?int?pageStartRow =?0;
?
????/***
?????* 每頁(yè)顯示數(shù)據(jù)的終止數(shù)
?????*/
????private?int?pageEndRow =?0;
?
????/***
?????* 是否有下一頁(yè)
?????*/
????private?boolean?hasNextPage =?false;
?
????/***
?????* 是否有前一頁(yè)
?????*/
????private?boolean?hasPreviousPage =?false;
?
????/***
?????* 數(shù)據(jù)集合
?????*/
????private?List<T> list;
?
?
????public?PageModel(List<T> list,?int?pageRecorders) {
????????// 通過對(duì)象集,記錄總數(shù)劃分
????????init(list, pageRecorders);
????}
?
????/** *//**
?????* 初始化list,并告之該list每頁(yè)的記錄數(shù)
?????* @param list 數(shù)據(jù)幾個(gè)
?????* @param pageRecorders 一頁(yè)顯示多少數(shù)據(jù)
?????*/
????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;
????????}
????}
?
?
????// 判斷要不要分頁(yè)
????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 =?"共有數(shù)據(jù)數(shù):"?+?this.getTotalRows() +
?
????????????????"共有頁(yè)數(shù): "?+?this.getTotalPages() +
?
????????????????"當(dāng)前頁(yè)數(shù)為:"?+?this.getPage() +
?
????????????????" 是否有前一頁(yè): "?+?this.isHasPreviousPage() +
?
????????????????" 是否有下一頁(yè):"?+?this.isHasNextPage() +
?
????????????????" 開始行數(shù):"?+?this.getPageStartRow() +
?
????????????????" 終止行數(shù):"?+?this.getPageEndRow();
?
????????System.out.println(description);
????}
?
????public?List getNextPage() {
????????page = page +?1;
?
????????disposePage();
?
????????System.out.println("用戶凋用的是第"?+ page +?"頁(yè)");
????????this.description();
????????return?getObjects(page);
????}
?
????/**
?????* 處理分頁(yè)
?????*/
????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);
????}
?
????/**
?????* 獲取第幾頁(yè)的內(nèi)容
?????*
?????* @param page 當(dāng)前頁(yè)面
?????* @return
?????*/
????public?List<T> getObjects(int?page) {
????????if(page ==?0){
????????????this.setPage(1);
????????}
????????else{
????????????this.setPage(page);
????????}
????????this.disposePage();
????????if?(page * pageRecorders < totalRows) {
????????????// 判斷是否為最后一頁(yè)
????????????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;
????}
?
}
?
?
?
?
測(cè)試類TestPagerModel?
?
import java.util.ArrayList;
import java.util.List;
/**
?* @version 1.0
?* @author: fwjia
?*/
public class TestPagerModel {
? ? public static void main(String args[]) {
? ? ? ? List<String> list = new ArrayList<String>();
? ? ? ? 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<String> pm = new PageModel(list, 5);
? ? ? ? List<String> sublist = pm.getObjects(2);
? ? ? ? for(int i = 0; i < sublist.size(); i++) {
? ? ? ? ? ? System.out.println(sublist.get(i));
? ? ? ? }
? ? }
}
?
?
?
上面的在項(xiàng)目中使用了
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
在java中實(shí)現(xiàn)list分頁(yè)
原創(chuàng)墨咖 最后發(fā)布于2019-06-29 17:12:54 閱讀數(shù) 162 ?收藏
展開
? ? ? ? ArrayList list = new ArrayList();
? ? ? ? list.add("name");
? ? ? ? list.add("age");
? ? ? ? list.add(16);
? ? ? ? list.add("name1");
? ? ? ? list.add("age1");
? ? ? ? list.add(161);
? ? ? ? list.add("name2");
? ? ? ? list.add("age2");
? ? ? ? list.add(162);
? ? ? ? System.out.println("分頁(yè)參數(shù):0,3--:"+list.subList(0,3));
? ? ? ? System.out.println("分頁(yè)參數(shù):3,6--:"+list.subList(3,6));
? ? ? ? System.out.println("分頁(yè)參數(shù):6,9--:"+list.subList(6,9));
?
————————————————
版權(quán)聲明:本文為CSDN博主「墨咖」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_37335810/article/details/94177407
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
/**
?* java內(nèi)存分頁(yè)工具類
?*/
public class JavaMemoryPageUtil {
??
??/**
???* 獲取分頁(yè)數(shù)據(jù)
???* @param dataList 進(jìn)行分頁(yè)的數(shù)據(jù)集合
???* @param pageNum? 第幾頁(yè)
???* @param pageSize 每頁(yè)顯示多少條
???* @return
???*/
??@SuppressWarnings({ "rawtypes", "unchecked" })
??public static List getPageLimit(List dataList, int pageNum, int pageSize){
????if(CollectionUtils.isEmpty(dataList)){
??????return dataList;
????}
????List resultList = new ArrayList();
????// 所有dataList數(shù)據(jù)中的第幾條
????int currIdx = pageNum > 1 ? (pageNum -1) * pageSize : 0;
????for (int i = 0; i < pageSize && i < dataList.size() - currIdx; i++) {
??????resultList.add(dataList.get(currIdx + i));
????}
????return resultList;
??}
}
?
總結(jié)
以上是生活随笔為你收集整理的JAVA List集合转Page(分页对象) java 分页 PageModel 测试类TestPagerModel的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 伸缩缝设置规范要求(浆砌石挡墙伸缩缝设置
- 下一篇: 魔兽切换天赋宏(魔兽世界技能重置宏)