近期在实现项目接口时,经常需要分页功能。有时返回“String”,有时是“Object”格式的分页List。针对这种情况,本人用java实现对List分页。
第一版
package org.sun.java;import java.util.ArrayList;import java.util.List;/** * * @author Administrator * @date 2016年7月16日 */public class SubListOperation { final static int COUNT = 18; public Listinit(int size) { List lists = new ArrayList (); for (int i = 0; i < size; i++) { lists.add("t" + (i + 1)); } return lists; } /** * * @param pageNum * @param pageSize * @return 简单分页功能 */ public List subList(int pageNum, int pageSize) { List lists = init(COUNT); int fromIndex = (pageNum - 1) * pageSize; int toIndex = fromIndex + pageSize; return lists.subList(fromIndex, toIndex); } public static void main(String[] args) { System.out.println("-------- start --------"); new SubListOperation().output(); System.out.println("-------- finish --------"); } public void output(){ int pageNum=2; int pageSize=5; for(String s:subList(pageNum,pageSize)){ System.out.println(s); } }}
-------- start --------
t6t7t8t9t10-------- finish --------考虑到超出元素个数时异常的情况,做多一步判断。
第二版
/** * * @param pageNum * @param pageSize * @return 简单分页功能 */ public ListsubList(int pageNum, int pageSize) { List lists = init(COUNT); int fromIndex = (pageNum - 1) * pageSize; int toIndex = fromIndex + pageSize; int size = lists.size(); if(fromIndex>=size){ return new ArrayList (); } return lists.subList(fromIndex, Math.min(size,toIndex)); } 第三版
package org.sun.java;import java.util.ArrayList;import java.util.List;/** * * @author Administrator * @date 2016年7月16日 */public class SubListOperation { final static int COUNT = 18; public Listinit(int size) { List lists = new ArrayList (); for (int i = 0; i < size; i++) { lists.add("t" + (i + 1)); } return lists; } /** * * @param size * @return 初始化用户列表 */ public List initUser(int size) { List lists = new ArrayList (); for (int i = 0; i < size; i++) { lists.add(new User((i + 1), "user" + (i + 1))); } return lists; } /** * * @param pageNum * @param pageSize * @return 简单分页功能 */ public List userSubList(int pageNum, int pageSize) { List lists = initUser(COUNT); int fromIndex = (pageNum - 1) * pageSize; int toIndex = fromIndex + pageSize; int size = lists.size(); if (fromIndex >= size) { return new ArrayList (); } return lists.subList(fromIndex, Math.min(size, toIndex)); } public List subList(int pageNum, int pageSize) { List lists = init(COUNT); int fromIndex = (pageNum - 1) * pageSize; int toIndex = fromIndex + pageSize; int size = lists.size(); if (fromIndex >= size) { return new ArrayList (); } return lists.subList(fromIndex, Math.min(size, toIndex)); } static class User { int id; String name; public User(int id, String name) { super(); this.id = id; this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } } public static void main(String[] args) { System.out.println("-------- start --------"); // new SubListOperation().outputString(); new SubListOperation().outputObject(); System.out.println("-------- finish --------"); } public void outputString() { int pageNum = 2; int pageSize = 5; for (String s : subList(pageNum, pageSize)) { System.out.println(s); } } public void outputObject() { int pageNum = 2; int pageSize = 5; for (User s : userSubList(pageNum, pageSize)) { System.out.println(s); } }}
-------- start --------
User [id=6, name=user6]User [id=7, name=user7]User [id=8, name=user8]User [id=9, name=user9]User [id=10, name=user10]-------- finish --------
泛型方法,通用类型实现分页
package org.sun.java;import java.util.ArrayList;import java.util.List;/** * * @author Administrator * @date 2016年7月16日 */public class SubListOperation { final static int COUNT = 18; public Listinit(int size) { List lists = new ArrayList (); for (int i = 0; i < size; i++) { lists.add("t" + (i + 1)); } return lists; } /** * * @param size * @return 初始化用户列表 */ public List initUser(int size) { List lists = new ArrayList (); for (int i = 0; i < size; i++) { lists.add(new User((i + 1), "user" + (i + 1))); } return lists; } /** * * @param lists 将被分页展示的列表 * @param pageNum 页码数 * @param pageSize 每页条数大小 * @return */ public List generalPagenation(List lists,int pageNum, int pageSize) { int fromIndex = (pageNum - 1) * pageSize; int toIndex = fromIndex + pageSize; int size = lists.size(); if (fromIndex >= size) { return new ArrayList (); } return lists.subList(fromIndex, Math.min(size, toIndex)); } static class User { int id; String name; public User(int id, String name) { super(); this.id = id; this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } } public static void main(String[] args) { System.out.println("-------- start --------"); new SubListOperation().outputT(); System.out.println("-------- finish --------"); } public void outputT() { int pageNum = 3; int pageSize = 5; for (Object s : generalPagenation(init(18),pageNum, pageSize)) { System.out.println(s); } System.out.println("---------***---------"); for (Object s : generalPagenation(initUser(12),pageNum, pageSize)) { System.out.println(s); } }}
-------- start --------
t11t12t13t14t15---------***---------User [id=11, name=user11]User [id=12, name=user12]-------- finish --------