fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone {
  6. public static void main (String[] args) throws java.lang.Exception {
  7. List<String> initialCollection = new ArrayList<String>();
  8. for(int i = 0; i < 31; i++) {
  9. initialCollection.add("" + i);
  10. }
  11. List<List<String>> resultCollections = new ArrayList<>();
  12.  
  13. int totalSize = initialCollection.size();
  14. int parts = 5;
  15. int baseSize = totalSize / parts;
  16. int remainder = totalSize % parts;
  17.  
  18. int startIndex = 0;
  19. for (int i = 0; i < parts; i++) {
  20. int endIndex = startIndex + baseSize + (i < remainder ? 1 : 0);
  21. List<String> part = new ArrayList<>(initialCollection.subList(startIndex, endIndex));
  22. resultCollections.add(part);
  23. startIndex = endIndex;
  24. }
  25.  
  26. // Print the results
  27. for (List<String> part : resultCollections) {
  28. System.out.println(part.size() + ": " + part);
  29. }
  30. }
  31. }
Success #stdin #stdout 0.21s 57812KB
stdin
Standard input is empty
stdout
7: [0, 1, 2, 3, 4, 5, 6]
6: [7, 8, 9, 10, 11, 12]
6: [13, 14, 15, 16, 17, 18]
6: [19, 20, 21, 22, 23, 24]
6: [25, 26, 27, 28, 29, 30]