Here is an example using a java stream ,
public static void main(String[] args) throws Exception { List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)); int batchSize = 5; AtomicInteger ai = new AtomicInteger(); Collection<List<Integer>> chunkedOrders = list.stream() .collect(Collectors.groupingBy(item -> ai.getAndIncrement() / batchSize)).values(); System.out.println("Your innerlist = " + chunkedOrders); chunkedOrders.forEach(chunk -> { System.out.println("Processing" + " " + chunk.size() + " " + " data, sublist = " + chunk); }); }
Output:
Your innerlist = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11]] Processing 5 data, sublist = [1, 2, 3, 4, 5] Processing 5 data, sublist = [6, 7, 8, 9, 10] Processing 1 data, sublist = [11]