user8838577
user8838577

Reputation:

Java 8 way to convert int[][] to Integer[][]

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

Answers (2)

user14940971
user14940971

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

Michael
Michael

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

Related Questions