Это сделает это. В этом примере используется ArrayLists
, но он также будет работать с LinkedLists
или всем, что реализует интерфейс List
.
Создание списка списков.
ArrayList<List<Integer>> stacks =
new ArrayList<>(List.of(List.of(22, 1, 7),
List.of(111, 112), List.of(44, 123, 99)));
и преобразование в int array
int[] ints = stacks
.stream() // convert to a stream of lists
.flatMap(List::stream) // combine all lists to one list of Integers
.mapToInt(Integer::intValue)// convert Integers to ints
.toArray(); // and output to an array.
System.out.println(Arrays.toString(ints));
Отпечатки
[22, 1, 7, 111, 112, 44, 123, 99]