fork(11) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.stream.Collectors;
  7. import java.util.stream.IntStream;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception {
  13. List<Integer> l = Arrays.asList(1,2,3,4,5,5,6,6,7,7,7,8,3,8,8);
  14. final int N=3;
  15. System.out.println(nSizeParts(l,N));
  16. System.out.println(nTimeParts(l,N));
  17. }
  18. private static <T> List<List<T>> nSizeParts(List<T> objs, final int N) {
  19. return new ArrayList<>(IntStream.range(0, objs.size()).boxed().collect(
  20. Collectors.groupingBy(e->e/N,Collectors.mapping(e->objs.get(e), Collectors.toList())
  21. )).values());
  22. }
  23. private static <T> List<List<T>> nTimeParts(List<T> objs, final int N) {
  24. return new ArrayList<>(IntStream.range(0, objs.size()).boxed().collect(
  25. Collectors.groupingBy(e->e%N,Collectors.mapping(e->objs.get(e), Collectors.toList())
  26. )).values());
  27. }
  28. }
Success #stdin #stdout 0.28s 34160KB
stdin
Standard input is empty
stdout
[[1, 2, 3], [4, 5, 5], [6, 6, 7], [7, 7, 8], [3, 8, 8]]
[[1, 4, 6, 7, 3], [2, 5, 6, 7, 8], [3, 5, 7, 8, 8]]