Java map an array of Strings to an array of Integers

Fiodor :

I found this code on SO to map strings to ints

Arrays.stream(myarray).mapToInt(Integer::parseInt).toArray();

But how do I make it map to Integer type not the primitive int?

I tried switching from Integer.parseInt to Integer.valueOf, but it seems that the mapToInt() method forces the primitive type.

I have an ArrayList of arrays of Integers, so I cannot use primitive ints.

Flown :

Since String and Integer are both reference types you can simply call Stream::map to transform your array.

Integer[] boxed = Stream.of(myarray).map(Integer::valueOf).toArray(Integer[]::new);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=471178&siteId=1