我有对象列表。我需要做分页。
输入参数是每页的最大对象数和页码。
例如输入list = ("a", "b", "c", "d", "e", "f")
每页最大页数为 2 页数为 2 结果 = ("c", "d")
是否有任何现成的类(库)来做到这一点?比如Apache项目等等。
我有对象列表。我需要做分页。
输入参数是每页的最大对象数和页码。
例如输入list = ("a", "b", "c", "d", "e", "f")
每页最大页数为 2 页数为 2 结果 = ("c", "d")
是否有任何现成的类(库)来做到这一点?比如Apache项目等等。
int sizePerPage=2;
int page=2;
int from = Math.max(0,page*sizePerPage);
int to = Math.min(list.size(),(page+1)*sizePerPage)
list.subList(from,to)
使用 Java 8 流:
list.stream()
.skip(page * size)
.limit(size)
.collect(Collectors.toCollection(ArrayList::new));
尝试:
int page = 1; // starts with 0, so we on the 2nd page
int perPage = 2;
String[] list = new String[] {"a", "b", "c", "d", "e", "f"};
String[] subList = null;
int size = list.length;
int from = page * perPage;
int to = (page + 1) * perPage;
to = to < size ? to : size;
if ( from < size ) {
subList = Arrays.copyOfRange(list, from, to);
}
简单的方法
public static <T> List<T> paginate(Page page, List<T> list) {
int fromIndex = (page.getNumPage() - 1) * page.getLenght();
int toIndex = fromIndex + page.getLenght();
if (toIndex > list.size()) {
toIndex = list.size();
}
if (fromIndex > toIndex) {
fromIndex = toIndex;
}
return list.subList(fromIndex, toIndex);
}
试试这个:
int pagesize = 2;
int currentpage = 2;
list.subList(pagesize*(currentpage-1), pagesize*currentpage);
此代码返回一个列表,其中仅包含您想要的元素(页面)。
您还应该检查索引以避免 java.lang.IndexOutOfBoundsException。
根据您的问题,简单List.subList
会给您预期的行为 size()/ 2= 页数
您可以使用List.subList
usingMath.min
来防范ArrayIndexOutOfBoundsException
:
List<String> list = Arrays.asList("a", "b", "c", "d", "e");
int pageSize = 2;
for (int i=0; i < list.size(); i += pageSize) {
System.out.println(list.subList(i, Math.min(list.size(), i + pageSize)));
}
使用 lombok 作为 getter 和 setter
@Getter
@Setter
public class Pagination<T> {
private int pageSize = 10;
private int paginationSize = 9;
private int currentPage = 0;
private List<T> list = null;
public Pagination(List<T> list) {
this.list = list;
}
public int getSize() {
return list == null ? 0 : list.size();
}
public int getOffset() {
return getPageSize() * getCurrentPage();
}
public int getLimit() {
return getPageSize();
}
public List<T> getItemsOnCurrentPage() {
return list.subList(getOffset(), getOffset()+getLimit());
}
public int getPageCount() {
return getSize() / getPageSize() + (getSize() % getPageSize() > 0 ? 1 : 0);
}
public int getActualPaginationSize() {
return Math.min(getPaginationSize(), getPageCount());
}
public int getLastPage() {
return getPageCount() - 1;
}
public int getStartPage() {
if (getPageCount() > getPaginationSize()
&& getCurrentPage() > getPaginationSize() / 2) {
return Math.min(getCurrentPage() - getPaginationSize() / 2,
getPageCount() - getActualPaginationSize());
} else {
return 0;
}
}
public int getEndPage() {
return Math.min(getStartPage() + getPaginationSize(), getPageCount()) - 1;
}
public void setCurrentPage(int newPage) {
if (newPage < 0) newPage = 0;
if (newPage > getLastPage()) newPage = getLastPage();
this.currentPage = newPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage();
}
public boolean hasPreviousPage() {
return getStartPage() > 0;
}
public boolean isHasNextPage() {
return hasNextPage();
}
public boolean hasNextPage() {
return getEndPage() < getLastPage();
}
public boolean isShouldShowFirst() {
return shouldShowFirst();
}
public boolean shouldShowFirst() {
return getStartPage() - getPaginationSize() / 2 > 0;
}
public boolean isShouldShowLast() {
return shouldShowLast();
}
public boolean shouldShowLast() {
return getEndPage() + getPaginationSize() / 2 < getLastPage();
}
}
页面索引从 0 开始,当前页面居中。