Efficient way to divide a list into lists of n size
I have an ArrayList
, which I want to divide into smaller List
objects of n
size, and perform an operation on each.
My current method of doing this is implemented with ArrayList
objects in Java. Any pseudocode will do.
for (int i = 1; i <= Math.floor((A.size() / n)); i++) {
ArrayList temp = subArray(A, ((i * n) - n),
(i * n) - 1);
// do stuff with temp
}
private ArrayList<Comparable> subArray(ArrayList A, int start,
int end) {
ArrayList toReturn = new ArrayList();
for (int i = start; i <= end; i++) {
toReturn.add(A.get(i));
}
return toReturn;
}
where A
is the list and n
is the size of the desired lists
I believe this way is taking too much time when working with considerably large lists of up to 1 million in size, so I'm trying to figure out what would be more efficient.
You'll want to do something that makes use of List.subList(int, int) views rather than copying each sublist. To do this really easily, use Guava's Lists.partition(List, int) method:
List<Foo> foos = ...
for (List<Foo> partition : Lists.partition(foos, n)) {
// do something with partition
}
Note that this, like many things, isn't very efficient with a List
that isn't RandomAccess
(such as a LinkedList
).
For example:
int partitionSize = 10;
List<List<String>> partitions = new ArrayList<>();
for (int i=0; i<yourlist.size(); i += partitionSize) {
partitions.add(yourlist.subList(i, Math.min(i + partitionSize, yourlist.size())));
}
for (List<String> list : partitions) {
//Do your stuff on each sub list
}
If you are working with a list I use the "Apache Commons Collections 4" library. It has a partition method in the ListUtils class:
...
int targetSize = 100;
List<Integer> largeList = ...
List<List<Integer>> output = ListUtils.partition(largeList, targetSize);
This method is adapted from http://code.google.com/p/guava-libraries/