This commit is contained in:
Looly 2021-08-17 21:57:41 +08:00
parent c6291993f9
commit 2b04713104
3 changed files with 15 additions and 17 deletions

View File

@ -7,6 +7,8 @@
### 🐣新特性
* 【core 】 增加NamingCase类
* 【core 】 ListUtil增加page方法重载pr#1761@Github
*
### 🐞Bug修复
-------------------------------------------------------------------------------------------------------------

View File

@ -2,6 +2,7 @@ package cn.hutool.core.collection;
import cn.hutool.core.comparator.PinyinComparator;
import cn.hutool.core.comparator.PropertyComparator;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Matcher;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
@ -280,29 +281,24 @@ public class ListUtil {
* @param list 源数据列表
* @param pageSize 每页的条目数
* @param pageListConsumer 单页数据函数式返回
* @since 5.7.10
*/
public static <T> void page(List<T> list, int pageSize, Consumer<List<T>> pageListConsumer) {
if (CollUtil.isEmpty(list)) {
if (CollUtil.isEmpty(list) || pageSize <= 0) {
return;
}
if (pageSize <= 0) {
return;
}
PageUtil.setFirstPageNo(0);
int size = list.size();
int page = PageUtil.totalPage(size, pageSize);
for (int pageNo = PageUtil.getFirstPageNo(); pageNo < page; pageNo++) {
final int total = list.size();
final int totalPage = PageUtil.totalPage(total, pageSize);
for (int pageNo = PageUtil.getFirstPageNo(); pageNo < totalPage + PageUtil.getFirstPageNo(); pageNo++) {
// 获取当前页在列表中对应的起止序号
int[] startEnd = PageUtil.transToStartEnd(pageNo, pageSize);
int start = startEnd[0];
int end = startEnd[1];
if (end > size) {
end = size;
final int[] startEnd = PageUtil.transToStartEnd(pageNo, pageSize);
if (startEnd[1] > total) {
startEnd[1] = total;
}
// 使用拷贝防止对返回分页数据进行增删操作时影响源数据的分页结果
CopyOnWriteArrayList<T> pageList = ListUtil.toCopyOnWriteArrayList(list.subList(start, end));
// 返回数据
pageListConsumer.accept(pageList);
pageListConsumer.accept(sub(list, startEnd[0], startEnd[1]));
}
}

View File

@ -101,7 +101,7 @@ public class ListUtilTest {
int[] d1 = ListUtil.page(0,8,a).stream().mapToInt(Integer::valueOf).toArray();
Assert.assertArrayEquals(new int[]{1,2,3,4,5},d1);
// page with consumer
List<List<Integer>> pageListData = new ArrayList<>();
ListUtil.page(a, 2, pageListData::add);
Assert.assertArrayEquals(new int[]{1, 2}, pageListData.get(0).stream().mapToInt(Integer::valueOf).toArray());