This commit is contained in:
Looly 2023-03-29 21:57:20 +08:00
parent 59d264048f
commit a0ac4362dd
2 changed files with 11 additions and 2 deletions

View File

@ -844,11 +844,12 @@ public class CollUtil {
return result;
}
ArrayList<T> subList = new ArrayList<>(size);
final int initSize = Math.min(collection.size(), size);
ArrayList<T> subList = new ArrayList<>(initSize);
for (final T t : collection) {
if (subList.size() >= size) {
result.add(subList);
subList = new ArrayList<>(size);
subList = new ArrayList<>(initSize);
}
subList.add(t);
}

View File

@ -304,6 +304,14 @@ public class CollUtilTest {
Assert.assertEquals(3, split.get(0).size());
}
@Test
public void splitTest2() {
final ArrayList<Integer> list = ListUtil.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
final List<List<Integer>> split = CollUtil.split(list, Integer.MAX_VALUE);
Assert.assertEquals(1, split.size());
Assert.assertEquals(9, split.get(0).size());
}
@Test
public void foreachTest() {
final HashMap<String, String> map = MapUtil.newHashMap();