Rowhawn Rowhawn - 3 months ago 23
Java Question

Efficient way to divide a list into lists of n size

I have an array, which I want to divide into smaller arrays of n size, and perform an operation on each.
My current method of doing this is

implemented with ArrayLists 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, 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.

Answer

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).