aad ListUtil.split

This commit is contained in:
Looly 2020-10-14 15:33:02 +08:00
parent cc59412909
commit 8ae81241be
4 changed files with 23 additions and 10 deletions

View File

@ -17,6 +17,7 @@
* 【core 】 DataSizeUtil支持小数pr#1158@Github
* 【core 】 完善注释pr#193@Gitee
* 【core 】 优化Combination.countAllpr#1159@Github
* 【core 】 优化针对list的split方法pr#194@Gitee
### Bug修复
* 【core 】 解决农历判断节日未判断大小月导致的问题issue#I1XHSF@Gitee

View File

@ -1117,12 +1117,16 @@ public class CollUtil {
/**
* 对集合按照指定长度分段每一个段为单独的集合返回这个集合的列表
* <p>
* 需要特别注意的是此方法调用{@link List#subList(int, int)}切分List
* 此方法返回的是原List的视图也就是说原List有变更切分后的结果也会变更
* </p>
*
* @param <T> 集合元素类型
* @param <T> 集合元素类型
* @param list 列表
* @param size 每个段的长度
*
* @return 分段列表
* @since 5.4.5
*/
public static <T> List<List<T>> splitList(List<T> list, int size) {
return ListUtil.split(list, size);

View File

@ -253,7 +253,7 @@ public class ListUtil {
}
}
// 相乘可能会导致越界 临时用long
if (((long)pageNo * pageSize) > resultSize) {
if (((long) pageNo * pageSize) > resultSize) {
// 越界直接返回空
return new ArrayList<>(0);
}
@ -478,7 +478,7 @@ public class ListUtil {
* @since 5.2.6
*/
public static <T> List<T> unmodifiable(List<T> list) {
if(null == list){
if (null == list) {
return null;
}
return Collections.unmodifiableList(list);
@ -498,24 +498,30 @@ public class ListUtil {
/**
* 对集合按照指定长度分段每一个段为单独的集合返回这个集合的列表
*
* @param <T> 集合元素类型
* <p>
* 需要特别注意的是此方法调用{@link List#subList(int, int)}切分List
* 此方法返回的是原List的视图也就是说原List有变更切分后的结果也会变更
* </p>
*
* @param <T> 集合元素类型
* @param list 列表
* @param size 每个段的长度
*
* @return 分段列表
* @since 5.4.5
*/
public static <T> List<List<T>> split(List<T> list, int size) {
if (CollUtil.isEmpty(list)) {
return Collections.emptyList();
}
List<List<T>> result = new ArrayList<>(list.size() / size + 1);
final int listSize = list.size();
final List<List<T>> result = new ArrayList<>(listSize / size + 1);
int offset = 0;
for (int toIdx = size; toIdx <= list.size(); offset = toIdx, toIdx += size) {
for (int toIdx = size; toIdx <= listSize; offset = toIdx, toIdx += size) {
result.add(list.subList(offset, toIdx));
}
if (offset < list.size()) {
result.add(list.subList(offset, list.size()));
if (offset < listSize) {
result.add(list.subList(offset, listSize));
}
return result;
}

View File

@ -4,6 +4,7 @@ import cn.hutool.core.date.StopWatch;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.RandomUtil;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.util.ArrayList;
@ -12,6 +13,7 @@ import java.util.List;
public class ListUtilTest {
@Test
@Ignore
public void split() {
List<String> list = new ArrayList<>();
CollUtil.padRight(list, RandomUtil.randomInt(1000_0000, 1_0000_0000), "test");