Skip to content

Instantly share code, notes, and snippets.

@jottinger
Last active August 29, 2015 14:07
Show Gist options
  • Save jottinger/9b340a6690e1d6a0cadc to your computer and use it in GitHub Desktop.
Save jottinger/9b340a6690e1d6a0cadc to your computer and use it in GitHub Desktop.
package org.javachannel;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
public class LemonSparrowExample {
public static void main(String[] args) {
List<Integer> ids = new ArrayList<>();
IntStream.range(0, 2300).forEach(ids::add);
List<List<Integer>> splitList = split(ids, 1000);
System.out.println("Number of lists: " + splitList.size());
splitList.stream().forEach(l -> {
System.out.println("Size of sublist: " + l.size());
});
System.out.println("Size of original list: " + ids.size());
for (List<Integer> aSplitList : splitList) {
for (int i = 0; i < 5; i++) {
System.out.println(aSplitList.get(i));
}
}
}
private static List<List<Integer>> split(List<Integer> ids, int sizeOfList) {
List<Integer> tempList = new ArrayList<>(ids);
List<List<Integer>> returnedList = new ArrayList<>();
while (tempList.size() > 0) {
int end = Math.min(sizeOfList, tempList.size());
List<Integer> subList = tempList.subList(0, end);
returnedList.add(new ArrayList<>(subList));
subList.clear();
}
return returnedList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment