Convert object array to string array in Java
This post will discuss how to convert the object array to a string array in Java.
To convert object array of same type as String
1. Using System.arraycopy() method
We can use System.arraycopy() that copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; class Main { // Program to convert the object array to a string array in Java public static void main(String[] args) { Object[] src = { "NYC", "Washington DC", "New Delhi" }; String[] dest = new String[src.length]; System.arraycopy(src, 0, dest, 0, src.length); System.out.println(Arrays.toString(dest)); } } |
Output:
[NYC, Washington DC, New Delhi]
2. Using Arrays.copyOf() method
We can also use Arrays.copyOf() to copy the specified array to an array of the specified type.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; class Main { // Program to convert the object array to a string array in Java public static void main(String[] args) { Object[] objectArray = { "NYC", "Washington DC", "New Delhi" }; String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class); System.out.println(Arrays.toString(stringArray)); } } |
Output:
[NYC, Washington DC, New Delhi]
3. Using List.toArray() method
Here we first convert the object array to a list of objects and then use the toArray(T[]) method to dump the list into a newly allocated array of String.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; class Main { // Program to convert the object array to a string array in Java public static void main(String[] args) { Object[] objectArray = { "NYC", "Washington DC", "New Delhi" }; String[] stringArray = Arrays.asList(objectArray) .toArray(new String[0]); // .toArray(new String[objectArray.length]); System.out.println(Arrays.toString(stringArray)); } } |
Output:
[NYC, Washington DC, New Delhi]
4. Using Java 8
In Java 8, we can use Stream to convert object array to string array easily. The idea is first to convert the specified object array to a sequential Stream and then use the toArray() method to accumulate the stream elements into a new string array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; class Main { // Program to convert the object array to a string array in Java 8 and above public static void main(String[] args) { Object[] objectArray = { "NYC", "Washington DC", "New Delhi" }; String[] stringArray = Arrays.stream(objectArray) .toArray(String[]::new); System.out.println(Arrays.toString(stringArray)); } } |
Output:
[NYC, Washington DC, New Delhi]
To convert object array of other types than String
1. Naive solution
To convert an object array of other types than string, one approach uses a regular for-loop to iterate over the object array, and for every object, we cast it to string and assign it to the string array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; class Main { // Program to convert the object array to a string array in Java public static void main(String[] args) { Object[] objectArray = { 1, 2, 3, 4, 5 }; String[] stringArray = new String[objectArray.length]; // copy elements from object array to string array for (int i = 0; i < objectArray.length; i++) { stringArray[i] = String.valueOf(objectArray[i]); } System.out.println(Arrays.toString(stringArray)); } } |
Output:
[1, 2, 3, 4, 5]
2. Using Java 8
This is similar to the Java 8 approach discussed earlier, except here we call the Stream.map() method to convert every object in the stream to their string representation before calling the toArray() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; class Main { // Program to convert the object array to a string array in Java public static void main(String[] args) { Object[] objectArray = { 1, 2, 3, 4, 5 }; String[] stringArray = Arrays.stream(objectArray) .map(Object::toString) .toArray(String[]::new); System.out.println(Arrays.toString(stringArray)); } } |
Output:
[1, 2, 3, 4, 5]
That’s all about converting object array to string array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)