mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add page
This commit is contained in:
parent
c6291993f9
commit
2b04713104
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 增加NamingCase类
|
* 【core 】 增加NamingCase类
|
||||||
|
* 【core 】 ListUtil增加page方法重载(pr#1761@Github)
|
||||||
|
*
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
@ -2,6 +2,7 @@ package cn.hutool.core.collection;
|
|||||||
|
|
||||||
import cn.hutool.core.comparator.PinyinComparator;
|
import cn.hutool.core.comparator.PinyinComparator;
|
||||||
import cn.hutool.core.comparator.PropertyComparator;
|
import cn.hutool.core.comparator.PropertyComparator;
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
import cn.hutool.core.lang.Matcher;
|
import cn.hutool.core.lang.Matcher;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
@ -280,29 +281,24 @@ public class ListUtil {
|
|||||||
* @param list 源数据列表
|
* @param list 源数据列表
|
||||||
* @param pageSize 每页的条目数
|
* @param pageSize 每页的条目数
|
||||||
* @param pageListConsumer 单页数据函数式返回
|
* @param pageListConsumer 单页数据函数式返回
|
||||||
|
* @since 5.7.10
|
||||||
*/
|
*/
|
||||||
public static <T> void page(List<T> list, int pageSize, Consumer<List<T>> pageListConsumer) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
if (pageSize <= 0) {
|
|
||||||
return;
|
final int total = list.size();
|
||||||
}
|
final int totalPage = PageUtil.totalPage(total, pageSize);
|
||||||
PageUtil.setFirstPageNo(0);
|
for (int pageNo = PageUtil.getFirstPageNo(); pageNo < totalPage + PageUtil.getFirstPageNo(); pageNo++) {
|
||||||
int size = list.size();
|
|
||||||
int page = PageUtil.totalPage(size, pageSize);
|
|
||||||
for (int pageNo = PageUtil.getFirstPageNo(); pageNo < page; pageNo++) {
|
|
||||||
// 获取当前页在列表中对应的起止序号
|
// 获取当前页在列表中对应的起止序号
|
||||||
int[] startEnd = PageUtil.transToStartEnd(pageNo, pageSize);
|
final int[] startEnd = PageUtil.transToStartEnd(pageNo, pageSize);
|
||||||
int start = startEnd[0];
|
if (startEnd[1] > total) {
|
||||||
int end = startEnd[1];
|
startEnd[1] = total;
|
||||||
if (end > size) {
|
|
||||||
end = size;
|
|
||||||
}
|
}
|
||||||
// 使用拷贝,防止对返回分页数据进行增删操作时影响源数据的分页结果
|
|
||||||
CopyOnWriteArrayList<T> pageList = ListUtil.toCopyOnWriteArrayList(list.subList(start, end));
|
|
||||||
// 返回数据
|
// 返回数据
|
||||||
pageListConsumer.accept(pageList);
|
pageListConsumer.accept(sub(list, startEnd[0], startEnd[1]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ public class ListUtilTest {
|
|||||||
int[] d1 = ListUtil.page(0,8,a).stream().mapToInt(Integer::valueOf).toArray();
|
int[] d1 = ListUtil.page(0,8,a).stream().mapToInt(Integer::valueOf).toArray();
|
||||||
Assert.assertArrayEquals(new int[]{1,2,3,4,5},d1);
|
Assert.assertArrayEquals(new int[]{1,2,3,4,5},d1);
|
||||||
|
|
||||||
|
// page with consumer
|
||||||
List<List<Integer>> pageListData = new ArrayList<>();
|
List<List<Integer>> pageListData = new ArrayList<>();
|
||||||
ListUtil.page(a, 2, pageListData::add);
|
ListUtil.page(a, 2, pageListData::add);
|
||||||
Assert.assertArrayEquals(new int[]{1, 2}, pageListData.get(0).stream().mapToInt(Integer::valueOf).toArray());
|
Assert.assertArrayEquals(new int[]{1, 2}, pageListData.get(0).stream().mapToInt(Integer::valueOf).toArray());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user