对List集合进行分页
作者:互联网
1 简要说明
有时候,我们有一个list集合,需要对它进行分页处理
下面的根据类MyPageUtilVo就可以做到
它自带泛型,适合各种集合
可以设置每页的大小(默认为10)
根据页码(从1开始)就可以获取分页后的集合了
2 工具类
public class MyPageUtilVo<E> { /** * 每页显示的记录数 */ private int pageRecords = 10;</span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 总记录数 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> totalRecord; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 分页切割的启始点 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> startIndex; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 分页切割的结束点 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> endIndex; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 总页数 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> totalPage; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 当前页数 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> currentPage = <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 总记录集合 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> List<E><span style="color: #000000;"> totalList; </span><span style="color: #0000ff;">public</span> MyPageUtilVo(List<E><span style="color: #000000;"> totalList) { super(); </span><span style="color: #0000ff;">this</span>.totalList =<span style="color: #000000;"> totalList; innit(); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 初始化该分页对象 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> innit() { </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">null</span> !=<span style="color: #000000;"> totalList) { totalRecord </span>=<span style="color: #000000;"> totalList.size(); </span><span style="color: #0000ff;">if</span> (totalRecord % <span style="color: #0000ff;">this</span>.pageRecords == <span style="color: #800080;">0</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">this</span>.totalPage = totalRecord / <span style="color: #0000ff;">this</span><span style="color: #000000;">.pageRecords; } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">this</span>.totalPage = totalRecord / <span style="color: #0000ff;">this</span>.pageRecords + <span style="color: #800080;">1</span><span style="color: #000000;">; } } } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 得到分页后的数据 * * @return 分页数据 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> List<E> getPage(<span style="color: #0000ff;">int</span><span style="color: #000000;"> currentPage) { </span><span style="color: #0000ff;">this</span>.currentPage =<span style="color: #000000;"> currentPage; </span><span style="color: #0000ff;">if</span> (currentPage <= <span style="color: #800080;">0</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">this</span>.currentPage = <span style="color: #800080;">1</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (currentPage >= <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalPage) { </span><span style="color: #0000ff;">this</span>.currentPage = <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalPage; } List</span><E> subList = <span style="color: #0000ff;">new</span> ArrayList<E><span style="color: #000000;">(); </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">null</span> != <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalList) { subList.addAll(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.totalList.subList(getStartIndex(), getEndIndex())); } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> subList; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 设置每页显示的记录条数,如果不设置则默认为每页显示10条记录 * * @param pageRecords * 每页显示的记录条数(值必需介于10~100之间) </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> setPageRecords(<span style="color: #0000ff;">int</span><span style="color: #000000;"> pageRecords) { </span><span style="color: #0000ff;">if</span> (pageRecords >= <span style="color: #800080;">10</span> && pageRecords <= <span style="color: #800080;">100</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">this</span>.pageRecords =<span style="color: #000000;"> pageRecords; innit(); } } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getStartIndex() { </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">null</span> == <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalList) { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">this</span>.startIndex = (getCurrentPage() - <span style="color: #800080;">1</span>) * <span style="color: #0000ff;">this</span><span style="color: #000000;">.pageRecords; </span><span style="color: #0000ff;">if</span> (startIndex ><span style="color: #000000;"> totalRecord) { startIndex </span>=<span style="color: #000000;"> totalRecord; } </span><span style="color: #0000ff;">if</span> (startIndex < <span style="color: #800080;">0</span><span style="color: #000000;">) { startIndex </span>= <span style="color: #800080;">0</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> startIndex; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getEndIndex() { </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">null</span> == <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalList) { </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; } endIndex </span>= getStartIndex() + <span style="color: #0000ff;">this</span><span style="color: #000000;">.pageRecords; </span><span style="color: #0000ff;">if</span> (endIndex < <span style="color: #800080;">0</span><span style="color: #000000;">) { endIndex </span>= <span style="color: #800080;">0</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (endIndex <<span style="color: #000000;"> getStartIndex()) { endIndex </span>=<span style="color: #000000;"> getStartIndex(); } </span><span style="color: #0000ff;">if</span> (endIndex > <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalRecord) { endIndex </span>= <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalRecord; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> endIndex; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getTotalPage() { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> totalPage; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getTotalRecord() { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> totalRecord; } </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> boolean isEndPage() { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span>.currentPage == <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalPage; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 获取下一页的页数 * * @return 下一页的页数 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getNextPage() { </span><span style="color: #0000ff;">int</span> nextPage = <span style="color: #0000ff;">this</span>.currentPage + <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (nextPage > <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalPage) { nextPage </span>= <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalPage; } </span><span style="color: #0000ff;">if</span> (nextPage <= <span style="color: #800080;">0</span><span style="color: #000000;">) { nextPage </span>= <span style="color: #800080;">1</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> nextPage; } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 获取上一页的页数 * * @return 上一页的页数 </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getPrivyPage() { </span><span style="color: #0000ff;">int</span> privyPage = <span style="color: #0000ff;">this</span>.currentPage - <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (privyPage > <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalPage) { privyPage </span>= <span style="color: #0000ff;">this</span><span style="color: #000000;">.totalPage; } </span><span style="color: #0000ff;">if</span> (privyPage <= <span style="color: #800080;">0</span><span style="color: #000000;">) { privyPage </span>= <span style="color: #800080;">1</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> privyPage; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getCurrentPage() { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> currentPage; }
}
3 示例
//对一个List<Map<String,String>>进行分页
int size = list.size(); MyPageUtilVo<Map<String,String>> pa = new MyPageUtilVo(list); pa.setPageRecords(15); //设置每页数量 List<Map<String, String>> page = pa.getPage(1); return getDataTable(page,size);
标签:return,分页,int,List,pageRecords,lt,集合,currentPage,public 来源: https://www.cnblogs.com/jthr/p/16327519.html