User Tools

Site Tools


chunked_list_processing

Chunked List Processing

int blockSize = 3;

List<String> values = new ArrayList(Arrays.asList("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"));

while (!values.isEmpty()) {
    System.out.println("Size: " + values.size() + " : " + values);
    
    int size = values.size() > blockSize ? blockSize : values.size();
    
    List<String> subList = values.subList(0, size);
    System.out.println("Sublist: " + subList);
    
    values.subList(0, size).clear();
}

Result:

Size: 10 : [one, two, three, four, five, six, seven, eight, nine, ten]
Sublist: [one, two, three]
Size: 7 : [four, five, six, seven, eight, nine, ten]
Sublist: [four, five, six]
Size: 4 : [seven, eight, nine, ten]
Sublist: [seven, eight, nine]
Size: 1 : [ten]
Sublist: [ten]
chunked_list_processing.txt · Last modified: 2016/03/16 09:56 by mihael