mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
feat新增集合分类工具类,对集合进行分页,并返回一个分页对象,该对象包含分页数据和分页信息。
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package cn.hutool.v7.core.collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 集合分页工具类
|
||||
*
|
||||
* <p>用于对内存中的集合进行分页,并返回分页信息和数据。</p>
|
||||
*
|
||||
* @author Nic
|
||||
* @since 2025-11-30
|
||||
*/
|
||||
public class CollectionPager {
|
||||
|
||||
/**
|
||||
* 分页结果类
|
||||
*
|
||||
* @param <T> 数据类型
|
||||
*/
|
||||
public static class Page<T> {
|
||||
private final int pageNum; // 当前页码
|
||||
private final int pageSize; // 每页大小
|
||||
private final int totalPage; // 总页数
|
||||
private final int totalCount; // 总记录数
|
||||
private final List<T> data; // 当前页数据
|
||||
|
||||
public Page(int pageNum, int pageSize, int totalCount, List<T> data) {
|
||||
this.pageNum = pageNum;
|
||||
this.pageSize = pageSize;
|
||||
this.totalCount = totalCount;
|
||||
this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
// Getters
|
||||
public int getPageNum() {
|
||||
return pageNum;
|
||||
}
|
||||
|
||||
public int getPageSize() {
|
||||
return pageSize;
|
||||
}
|
||||
|
||||
public int getTotalPage() {
|
||||
return totalPage;
|
||||
}
|
||||
|
||||
public int getTotalCount() {
|
||||
return totalCount;
|
||||
}
|
||||
|
||||
public List<T> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否有上一页
|
||||
*/
|
||||
public boolean hasPrev() {
|
||||
return pageNum > 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否有下一页
|
||||
*/
|
||||
public boolean hasNext() {
|
||||
return pageNum < totalPage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上一页页码
|
||||
*/
|
||||
public int getPrevPage() {
|
||||
return hasPrev() ? pageNum - 1 : pageNum;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下一页页码
|
||||
*/
|
||||
public int getNextPage() {
|
||||
return hasNext() ? pageNum + 1 : pageNum;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对集合进行分页
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
* @param collection 集合
|
||||
* @param pageNum 页码,从1开始
|
||||
* @param pageSize 每页大小
|
||||
* @return 分页结果对象
|
||||
*/
|
||||
public static <T> Page<T> paginate(Collection<T> collection, int pageNum, int pageSize) {
|
||||
if (collection == null) {
|
||||
collection = Collections.emptyList();
|
||||
}
|
||||
|
||||
if (pageNum < 1) {
|
||||
pageNum = 1;
|
||||
}
|
||||
|
||||
if (pageSize < 1) {
|
||||
pageSize = 10;
|
||||
}
|
||||
|
||||
List<T> list = new ArrayList<>(collection);
|
||||
int totalCount = list.size();
|
||||
int fromIndex = (pageNum - 1) * pageSize;
|
||||
if (fromIndex >= totalCount) {
|
||||
return new Page<>(pageNum, pageSize, totalCount, Collections.emptyList());
|
||||
}
|
||||
|
||||
int toIndex = Math.min(fromIndex + pageSize, totalCount);
|
||||
List<T> pageData = list.subList(fromIndex, toIndex);
|
||||
return new Page<>(pageNum, pageSize, totalCount, new ArrayList<>(pageData));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cn.hutool.v7.core.collection;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.*;
|
||||
|
||||
public class CollectionPagerTest {
|
||||
|
||||
@Test
|
||||
public void testPage() {
|
||||
List<String> list = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
|
||||
CollectionPager.Page<String> page = CollectionPager.paginate(list, 2, 3);
|
||||
|
||||
// 获取第2页的数据,每页3条,那么应该返回:["d","e","f"]
|
||||
System.out.println(page.getData()); // [d, e, f]
|
||||
System.out.println(page.getPageNum()); // 2
|
||||
System.out.println(page.getPageSize()); // 3
|
||||
System.out.println(page.getTotalCount()); // 10
|
||||
System.out.println(page.getTotalPage()); // 4
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user