diff --git a/CHANGELOG.md b/CHANGELOG.md index b6058e6dd..0bebafc45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ * 【core 】 DataSizeUtil支持小数(pr#1158@Github) * 【core 】 完善注释(pr#193@Gitee) * 【core 】 优化Combination.countAll(pr#1159@Github) +* 【core 】 优化针对list的split方法(pr#194@Gitee) ### Bug修复 * 【core 】 解决农历判断节日未判断大小月导致的问题(issue#I1XHSF@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index 208667353..1b5f93afa 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -1117,12 +1117,16 @@ public class CollUtil { /** * 对集合按照指定长度分段,每一个段为单独的集合,返回这个集合的列表 + *

+ * 需要特别注意的是,此方法调用{@link List#subList(int, int)}切分List, + * 此方法返回的是原List的视图,也就是说原List有变更,切分后的结果也会变更。 + *

* - * @param 集合元素类型 + * @param 集合元素类型 * @param list 列表 * @param size 每个段的长度 - * * @return 分段列表 + * @since 5.4.5 */ public static List> splitList(List list, int size) { return ListUtil.split(list, size); diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java index 574d5dc6f..614d40ca7 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java @@ -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 List unmodifiable(List list) { - if(null == list){ + if (null == list) { return null; } return Collections.unmodifiableList(list); @@ -498,24 +498,30 @@ public class ListUtil { /** * 对集合按照指定长度分段,每一个段为单独的集合,返回这个集合的列表 * - * @param 集合元素类型 + *

+ * 需要特别注意的是,此方法调用{@link List#subList(int, int)}切分List, + * 此方法返回的是原List的视图,也就是说原List有变更,切分后的结果也会变更。 + *

+ * + * @param 集合元素类型 * @param list 列表 * @param size 每个段的长度 - * * @return 分段列表 + * @since 5.4.5 */ public static List> split(List list, int size) { if (CollUtil.isEmpty(list)) { return Collections.emptyList(); } - List> result = new ArrayList<>(list.size() / size + 1); + final int listSize = list.size(); + final List> 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; } diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java index 4739eeb3f..d0d8eae85 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/ListUtilTest.java @@ -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 list = new ArrayList<>(); CollUtil.padRight(list, RandomUtil.randomInt(1000_0000, 1_0000_0000), "test");