6

I am attempting to convert a 2D int array into a 2D String array with this code:

Arrays.stream(intArray).map(a -> 
    Arrays.stream(a).map(i -> 
        Integer.toString(i)).toArray()).toArray(String[][]::new);

but I get the compile-time error cannot convert from String to int when doing Integer.toString(i). I thought it could be because I'm collecting the results of streaming an int array in a String array, but doesn't map create a new Collection?

1 Answer 1

11

Arrays.stream on an int[] returns an IntStream, and to go from an int to a String or any other Object, you have to use the IntStream.mapToObj method, not the map method:

Arrays.stream(intArray).map(a -> 
    Arrays.stream(a).mapToObj(i -> 
        Integer.toString(i)).toArray(String[]::new)).toArray(String[][]::new);

The map method of IntStream is only used to map from an int to an int.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.