I've tried this StackOverflow answer's code, but I get the error Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>)
:
//data is int[][]
Arrays.stream(data)
.map(i -> Arrays.stream(i)
.collect(Collectors.toList()))
.collect(Collectors.toList());
Program 1: Arrays. stream() to convert string array to stream. Program 2: Arrays. stream() to convert int array to stream.
Use reshape() Function to Transform 1d Array to 2d Array The number of components within every dimension defines the form of the array. We may add or delete parameters or adjust the number of items within every dimension by using reshaping. To modify the layout of a NumPy ndarray, we will be using the reshape() method.
Arrays.stream
will go through each int[]
in the int[][]
.
You can convert an int[]
to an IntStream
.
Then, in order to convert a stream of int
s to a List<Integer>
,
you first need to box them.
Once boxed to Integer
s, you can collect them to a list.
And finally collect the stream of List<Integer>
into a list.
List<List<Integer>> list = Arrays.stream(data)
.map(row -> IntStream.of(row).boxed().collect(Collectors.toList()))
.collect(Collectors.toList());
Demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With