Convert int array to String array in Java

In this post, we will discuss how to convert int array to String array in Java.


 

1. Naive –

Naive solution would be to create an array of String type and use a regular for loop to assign values to it from primitive integer array after converting int to Strings.

Java

Download   Run Code

Output:

[1, 2, 3, 4, 5]

 

2. Java 8 –

Another solution would be to use Java 8 Stream API to convert int array to String array –

  1. Convert the specified primitive array to a IntStream using Arrays.stream() or IntStream.of() method.
     
  2. Convert each element of the Stream to a String using IntStream.mapToObj() function.
     
  3. Return a String array containing elements of this stream using Stream.toArray().

Java8

Download   Run Code

Output:

[1, 2, 3, 4, 5]

 

3. Using Regex –

This approach is not recommended.

Java

Download   Run Code

Output:

[1, 2, 3, 4, 5]

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz