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条记录数,显示第三页的记录集如下:





猜你喜欢

转载自blog.csdn.net/appleyk/article/details/80857330