1

I have a two-dimensional ArrayList of Integers (say list)
ArrayList<ArrayList<Integer>> list=new ArrayList<ArrayList<Integer>>() //assume values are assigned
I want to convert it to a two-dimensional array of primitive int in Java. How can I achieve that using steam as we do in the one-dimensional case

ArrayList<Integer> x =  new ArrayList<Integer>();
int[] arr = x.stream().mapToInt(i -> i).toArray();
1
  • Shouldn't x be a list of lists? Commented May 24, 2020 at 5:27

1 Answer 1

1

You can do it as follows:

int[][] arr = list.stream()
                  .map(l -> l.stream()
                             .mapToInt(Integer::intValue)
                             .toArray())
                  .toArray(int[][]::new);

Each inner List is mapped to an int[] (by first converting it to an IntStream), and then you convert your Stream<int[]> to an int[][].

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.