How can I split an ArrayList into several lists?
20,429
Solution 1
google-collections has Lists.partition(). You supply the size for each sublist.
Solution 2
To start, you may find List#subList()
useful. Here's a basic example:
public static void main(String... args) {
List<Integer> list = new ArrayList<Integer>();
list.add(0);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
int targetSize = 3;
List<List<Integer>> lists = split(list, targetSize);
System.out.println(lists); // [[0, 1, 2], [3, 4, 5], [6]]
}
public static <T extends Object> List<List<T>> split(List<T> list, int targetSize) {
List<List<T>> lists = new ArrayList<List<T>>();
for (int i = 0; i < list.size(); i += targetSize) {
lists.add(list.subList(i, Math.min(i + targetSize, list.size())));
}
return lists;
}
Note that I didn't use the splittedInto
as it doesn't make much sense in combination with targetSize
.
Author by
Arthur Ronald
IT analyst Feel free to contact me at arthurseveral [at] gmail [dot] com
Updated on August 14, 2020Comments
-
Arthur Ronald almost 4 years
See the following ArrayList:
List<Integer> values = new ArrayList<Integer>(); values.add(0); values.add(1); values.add(2); values.add(3); values.add(4); values.add(5); values.add(6);
So we have:
integerList.size(); // outputs 7 elements
I need to show a Google chart as follows:
To generate its values, I just call
StringUtils.join(values, ","); // outputs 0,1,2,3,4,5,6
It happens it supports up to 1000 pixel width. So if I have many values, I need to split my ArrayList into other ArrayLists to generate other charts. Something like:
Integer targetSize = 3; // And suppose each target ArrayList has size equal to 3 // ANSWER GOES HERE List<List<Integer>> output = SomeHelper.split(values, targetSize);
What Helper should I use to get my goal?
-
jdmichal over 14 yearsNitpick: Should probably provide targetSize to the ArrayList constructor, so that it allocates the proper size array off the bat and doesn't overallocate / resize later.
-
BalusC over 14 yearsNo, the
targetSize
is not the length of thelists
. It would have been thesplittedInto
which I didn't use. ThesubList
already takes this into account. -
BalusC over 14 years
new ArrayList<List<T>>((list.size() / targetSize) + 1)
would have been better. -
Arthur Ronald over 14 yearsI think people like reinvent the wheel
-
Arthur Ronald over 14 yearsIn fact, you are right. It does not make sense splittedInto. (+1)
-
jdmichal over 14 yearsOh, right. I completely read that code wrong. Thought the list was being made for the sub-list. Thanks for the corrections. As pointed out, the correct size can still be determined.
-
jdmichal over 14 yearsNo. The problem is that in many cases it's harder to find the wheel than to invent your own.
-
jdmichal over 14 yearsBTW, even that linked class duplicates functionality.
Lists.asList(...)
duplicatesjava.util.Arrays.asList()
. And there's severalnewXXX
methods that would function exactly the same if there was a space after the new, thereby invoking constructors instead. -
Arthur Ronald over 14 years@jdmichal it's harder to find the wheel than to invent your own (+1) for your comment
-
Kevin Bourrillion over 14 yearsjdmichal: not true in either case. 'List<TypeOfObject> list = Lists.newArrayList()' can be used in place of 'List<TypeOfObject> list = new ArrayList<TypeOfObject>()'. Many users strongly prefer to avoid the redundancy. Lists.asList() combines either an element and array, or two elements and an array, into a single list. If you read the documentation, you'll see that this is so, and why it's so. Arrays.asList() doesn't do that. What got you so interested in trying to discredit our library as being reinventions of wheels?
-
tjg184 about 11 yearsBe careful on the targetSize here. A 0 will cause an infinite loop. :)