java convert array to another type

www.laut‮‬turi.com
java convert array to another type

To convert an array to another type in Java, you can use the Arrays.stream method to create a stream from the array, and then use the appropriate map method to map the elements of the stream to the new type.

Here is an example of how you can convert an array of int values to an array of String values in Java:

int[] array = {1, 2, 3, 4, 5};
String[] stringArray = Arrays.stream(array).mapToObj(String::valueOf).toArray(String[]::new);

In this example, we have an array called array that contains int values. We use the Arrays.stream method to create a stream from the array, and then use the mapToObj method to map the elements of the stream to String values using the String.valueOf method. Finally, we use the toArray method to convert the stream to an array of String values called stringArray.

You can use a similar approach to convert an array to any other type in Java. Just make sure to use the appropriate map method and the correct conversion function for the new type.

Created Time:2017-11-03 00:14:51  Author:lautturi