Reputation:
How to convert java
int[][]
to Integer[][]
? .It seems easy to convert single dimensional primitive array to Object type single dimensional array using java stream.
For example
Integer[] result = IntStream.of( intarray ).boxed().toArray( Integer[]::new );
Is there any way for two dimensional array like above ?
Upvotes: 5
Views: 1232
Reputation:
You can use Arrays.stream
method:
int[][] arr1 = {};
Integer[][] arr2 = Arrays.stream(arr1)
.map(row -> Arrays.stream(row)
.boxed()
.toArray(Integer[]::new))
.toArray(Integer[][]::new);
Upvotes: 0
Reputation: 44150
You can do the same thing within a map
operation
int[][] twoDimenArray = {};
Integer[][] result = Stream.of(twoDimenArray)
.map(array -> IntStream.of(array).boxed().toArray(Integer[]::new))
.toArray(Integer[][]::new);
Upvotes: 11