Java -- 對List集合進行分頁
通過使用hdfs api將檔案系統根目錄下的檔案列表給列出來了,demo 如下
輸出json如下:
[ { "path": "/20150129101639203.jpg", "replication": 2, "len": 31513, "owner": "root", "group": "supergroup", "permission": "-rw-r--r--", "accessTime": 1529743877043, "modificationTime": 1529743877170, "blockSize": 134217728, "readAccess": true, "writeAccess": true, "executeAcess": true, "directory": false }, { "path": "/BB", "replication": 0, "len": 0, "owner": "root", "group": "supergroup", "permission": "-rwxr-xr-x", "accessTime": 0, "modificationTime": 1530167905016, "blockSize": 0, "readAccess": true, "writeAccess": true, "executeAcess": true, "directory": true }, { "path": "/CC", "replication": 0, "len": 0, "owner": "root", "group": "supergroup", "permission": "-rwxr-xr-x", "accessTime": 0, "modificationTime": 1530167678015, "blockSize": 0, "readAccess": true, "writeAccess": true, "executeAcess": true, "directory": true }, { "path": "/data", "replication": 0, "len": 0, "owner": "root", "group": "supergroup", "permission": "-rwxr-xr-x", "accessTime": 0, "modificationTime": 1530080026915, "blockSize": 0, "readAccess": true, "writeAccess": true, "executeAcess": true, "directory": true }, { "path": "/mydir", "replication": 0, "len": 0, "owner": "root", "group": "supergroup", "permission": "-rwxr-xr-x", "accessTime": 0, "modificationTime": 1530008789795, "blockSize": 0, "readAccess": true, "writeAccess": true, "executeAcess": true, "directory": true }, { "path": "/tmp", "replication": 0, "len": 0, "owner": "root", "group": "supergroup", "permission": "-rwxr-xr-x", "accessTime": 0, "modificationTime": 1530086873355, "blockSize": 0, "readAccess": true, "writeAccess": true, "executeAcess": true, "directory": true }, { "path": "/user", "replication": 0, "len": 0, "owner": "root", "group": "supergroup", "permission": "-rwx------", "accessTime": 0, "modificationTime": 1530071678560, "blockSize": 0, "readAccess": true, "writeAccess": true, "executeAcess": true, "directory": true } ]
想做到如下的分頁效果:
起初分頁用的是通用Mybatis分頁外掛PageHelper,但是在包裝List物件成Page物件的時候遇到一點小問題,其實也不小,很直接,很Mybatis,如下
結果,程式跑起來的時候,遇到了下面的異常(頭疼的型別轉換)
異常訊息:java.util.ArrayList cannot be cast to com.github.pagehelper.Page
後來覺醒過來,反問自己:你一個普通的List物件怎麼可能強制轉成Page物件呢?
那為什麼,Mybatis通用mapper查詢出來的list集合就可以包裝成Page物件呢?
為此,我專門斷點查看了一下,這個selectByExample的返回結果究竟是何方神聖,如下:
不查不知道,以前總以為它就是一個普通的List物件,現在知道了才發現,這傢伙原來是影藏的“高富帥”!
本來想用現成的分頁外掛PageHelper實現目錄檔案列表的分頁顯示,這倒好,用不了,索性自己寫個
網上關於List的分頁實現的方法就一個,就是利用其自身的sublist方法,對自己進行元素的切分split,但是大家都說,這種方法有個弊端,就是實現的分頁是“偽分頁”,意思就是,分頁前,先把所有的結果查詢來,然後在進行分割,但是mybatis的分頁外掛不一樣,人家是真正的要多少查多少
話又說回來,我又不是查詢幾萬或幾百萬的檔案列表,再說了,誰會在同一個目錄下面存幾萬個或幾百萬個檔案呢?一個目錄下存放幾千個檔案就已經很恐怖了,不排除也有這種情況,但是檔案的列表的資訊都是存成json的,這點內容在記憶體裡面開銷還是沒問題的,因此,果斷針對檔案目錄列表寫了個通用分頁類
package com.appleyk.paging;
import java.util.ArrayList;
import java.util.List;
import com.appleyk.model.FileStatusModel;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* 對List集合進行分頁 == 使用subList對list物件進行切割
* @author [email protected]
* @blob http://blog.csdn.net/appleyk
* @date 2018年6月29日-下午3:13:36
* @param <T>
*/
public class DPage<T> {
//當前頁
private Integer pageNum;
//總記錄數
private Integer total;
//總頁數
private Integer pages;
//當前頁顯示多少條記錄
@JsonIgnore
private Integer pageSize;
private List<T> list;
public DPage(List<T> list,Integer pageNum,Integer pageSize){
this.pageNum = pageNum;
this.pageSize = pageSize;
this.total = list.size();
//總記錄數和每頁顯示的記錄之間是否可以湊成整數(pages)
boolean full = total % pageSize == 0;
//分頁 == 根據pageSize(每頁顯示的記錄數)計算pages
if(!full){
//如果湊不成整數
this.pages = total/pageSize + 1;
}else{
//如果湊成整數
this.pages = total/pageSize;
}
int fromIndex = 0;
int toIndex = 0;
fromIndex = pageNum*pageSize-pageSize;
if(pageNum == 0){
throw new ArithmeticException("第0頁無法展示");
}else if(pageNum>pages){
//如果查詢的頁碼數大於總的頁碼數,list設定為[]
list = new ArrayList<>();
}else if(pageNum == pages){
//如果查詢的當前頁等於總頁數,直接索引到total處
toIndex = total;
}else{
//如果查詢的頁碼數小於總頁數,不用擔心切割List的時候toIndex索引會越界,直接等
toIndex = pageNum*pageSize;
}
if(list.size() == 0){
this.list = list;
}else{
this.list = list.subList(fromIndex, toIndex);
}
}
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
使用該類包裝list物件後的效果如下:
查詢結果如下:
對比hdfs檔案系統上的mydir目錄下的的檔案列表如下:
每頁1條記錄數,顯示第三頁的記錄集如下: