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
  • 11,553
  • 8
  • 64
  • 88
PAA
  • 1
  • 46
  • 174
  • 282

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
  • 44,416
  • 5
  • 53
  • 99