2

I am looking to convert the List<List<Integer>> into int[][] in Java 8. Any quick pointers?

I tried something like below, but I need int[][].

List<Integer> flat = arr.stream()
                        .flatMap(List::stream)
                        .collect(Collectors.toList());
Lii
  • 10,777
  • 7
  • 58
  • 79
PAA
  • 9,086
  • 25
  • 138
  • 220

1 Answers1

4

You can map to int the inner lists and collect them into int arrays, then collect the outer list as a 2D array:

int[][] flat = arr.stream()
        .map(a -> a.stream().mapToInt(Integer::intValue).toArray())
        .toArray(int[][]::new);
ernest_k
  • 42,928
  • 5
  • 50
  • 93