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]

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of