How to convert object array to string array in Java
To convert an object array to a string array in Java, you can use the toString()
method of the Object
class and the map()
method of the Stream
class.
Here is an example of how you can convert an object array to a string array in Java:
Object[] objectArray = {"item1", "item2", "item3"};
String[] stringArray = Arrays.stream(objectArray)
.map(Object::toString)
.toArray(String[]::new);
In this example, the objectArray
is an object array that contains elements of type String
. The Arrays.stream()
method is used to create a stream of the elements in the object array. The map()
method is used to apply the toString()
method to each element in the stream, and the toArray()
method is used to collect the elements into a string array.
You can also use the Arrays.asList()
method to convert the object array to a List
and then use the toArray()
method to convert the List
to an array:
Object[] objectArray = {"item1", "item2", "item3"};
String[] stringArray = Arrays.asList(objectArray).toArray(new String[0]);
I hope this helps! Let me know if you have any questions.