Skip to content

Commit d33176a

Browse files
author
zihluwang
committed
feat: add functionality to split a list into sub lists of specified size
1 parent f3e6e70 commit d33176a

File tree

1 file changed

+72
-0
lines changed
  • devkit-utils/src/main/java/com/onixbyte/devkit/utils

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2024-2025 OnixByte.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.onixbyte.devkit.utils;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
/**
24+
* A utility class for splitting a List into multiple sub lists, where each sublist has a maximum
25+
* number of elements specified by the user.
26+
*
27+
* @author siujamo
28+
*/
29+
public final class ListUtil {
30+
31+
/**
32+
* Splits a given List into a List of sub lists, where each sublist contains at most
33+
* {@code maxSize} elements. The original list is not modified, and new sub lists are created
34+
* to hold the partitioned data.
35+
* <p>
36+
* If the original list's size is less than or equal to {@code maxSize}, a single sublist
37+
* containing all elements is returned. If the list is empty, an empty list of sub lists
38+
* is returned.
39+
*
40+
* @param <T> the type of elements in the list
41+
* @param originalList the list to be split, must not be null
42+
* @param maxSize the maximum number of elements in each sublist, must be positive
43+
* @return a List of sub lists, where each sublist has at most {@code maxSize} elements
44+
* @throws IllegalArgumentException if {@code originalList} is null or {@code maxSize} is less
45+
* than or equal to 0
46+
*/
47+
public static <T> List<List<T>> splitList(List<T> originalList, int maxSize) {
48+
// check input
49+
if (originalList == null || maxSize <= 0) {
50+
throw new IllegalArgumentException("List cannot be null and maxSize must be positive");
51+
}
52+
53+
var result = new ArrayList<List<T>>();
54+
var size = originalList.size();
55+
56+
// if the original list is empty or smaller than maxSize, return it as a single sublist
57+
if (size <= maxSize) {
58+
result.add(new ArrayList<>(originalList));
59+
return result;
60+
}
61+
62+
// split the list
63+
for (var i = 0; i < size; i += maxSize) {
64+
var end = Math.min(i + maxSize, size); // ensure not to exceed list length
65+
List<T> subList = originalList.subList(i, end);
66+
result.add(new ArrayList<>(subList)); // create a new list to avoid reference issues
67+
}
68+
69+
return result;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)