Browse Source

aad ListUtil.split

Looly 5 years ago
parent
commit
8ae81241be

+ 1 - 0
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)

+ 6 - 2
hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java

@@ -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);

+ 14 - 8
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 <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;
 	}

+ 2 - 0
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<String> list = new ArrayList<>();
 		CollUtil.padRight(list, RandomUtil.randomInt(1000_0000, 1_0000_0000), "test");