1919
2020import java .util .ArrayList ;
2121import java .util .List ;
22+ import java .util .Objects ;
23+ import java .util .function .Supplier ;
2224
2325/**
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+ * A utility class providing static methods for manipulating lists.
2627 *
2728 * @author siujamo
29+ * @author zihluwang
2830 */
2931public final class ListUtil {
3032
3133 /**
3234 * Private constructor to prevent instantiation of this utility class.
33- * <p>
34- * This class provides static methods for list manipulation and is not intended to be
35- * instantiated. The private constructor ensures that no instances can be created, enforcing
36- * the utility nature of the class.
3735 */
3836 private ListUtil () {
3937 }
@@ -50,33 +48,67 @@ private ListUtil() {
5048 * @param <T> the type of elements in the list
5149 * @param originalList the list to be split, must not be null
5250 * @param maxSize the maximum number of elements in each sublist, must be positive
51+ * @param listFactory list factory
5352 * @return a List of sub lists, where each sublist has at most {@code maxSize} elements
5453 * @throws IllegalArgumentException if {@code originalList} is null or {@code maxSize} is less
5554 * than or equal to 0
5655 */
57- public static <T > List <List <T >> splitList (List <T > originalList , int maxSize ) {
56+ public static <T > List <List <T >> chunk (List <T > originalList , int maxSize , Supplier < List < T >> listFactory ) {
5857 // check input
59- if (originalList == null || maxSize <= 0 ) {
60- throw new IllegalArgumentException ("List cannot be null and maxSize must be positive" );
58+ if (Objects .isNull (originalList )) {
59+ throw new IllegalArgumentException ("List cannot be null" );
60+ }
61+
62+ if (maxSize <= 0 ) {
63+ throw new IllegalArgumentException ("Max size should be greater than 0" );
64+ }
65+
66+ if (Objects .isNull (listFactory )) {
67+ throw new IllegalArgumentException ("List factory cannot be null" );
6168 }
6269
6370 var result = new ArrayList <List <T >>();
6471 var size = originalList .size ();
6572
6673 // if the original list is empty or smaller than maxSize, return it as a single sublist
6774 if (size <= maxSize ) {
68- result .add (new ArrayList <>(originalList ));
75+ var singleSubList = listFactory .get ();
76+ singleSubList .addAll (originalList );
77+ result .add (singleSubList );
6978 return result ;
7079 }
7180
7281 // split the list
7382 for (var i = 0 ; i < size ; i += maxSize ) {
7483 var end = Math .min (i + maxSize , size ); // ensure not to exceed list length
75- List <T > subList = originalList .subList (i , end );
76- result .add (new ArrayList <>(subList )); // create a new list to avoid reference issues
84+ var subList = originalList .subList (i , end );
85+ var subListWrapper = listFactory .get ();
86+ subListWrapper .addAll (subList );
87+ result .add (subListWrapper ); // create a new list to avoid reference issues
7788 }
7889
7990 return result ;
8091 }
8192
93+ /**
94+ * Splits a given List into a List of sub lists, where each sublist contains at most
95+ * {@code maxSize} elements. The original list is not modified, and new sub lists are created
96+ * to hold the partitioned data.
97+ * <p>
98+ * If the original list's size is less than or equal to {@code maxSize}, a single sublist
99+ * containing all elements is returned. If the list is empty, an empty list of sub lists
100+ * is returned.
101+ *
102+ * @param <T> the type of elements in the list
103+ * @param originalList the list to be split, must not be null
104+ * @param maxSize the maximum number of elements in each sublist, must be positive
105+ * @return a List of sub lists, where each sublist has at most {@code maxSize} elements
106+ * @throws IllegalArgumentException if {@code originalList} is null or {@code maxSize} is less
107+ * than or equal to 0
108+ * @see #chunk(List, int, Supplier)
109+ */
110+ public static <T > List <List <T >> chunk (List <T > originalList , int maxSize ) {
111+ return chunk (originalList , maxSize , ArrayList ::new );
112+ }
113+
82114}
0 commit comments