In this post, we’ll explore the process of converting Java arrays to and from other collection objects. Java arrays, being fixed-size data structures, often need to be converted to other Java objects like Strings or ArrayLists. Conversely, there are scenarios where these converted objects need to be transformed back into Java arrays. As Java arrays are a straightforward data structure, not all methods of portability are universally applicable. Therefore, it is essential to understand the workarounds for effective conversion to and from Java arrays. Let’s delve into the techniques of utilizing Java arrays in this context
Java Array to List
In Java, an array can be converted to an ArrayList. An ArrayList is a dynamic array in Java that stores objects of the same data type in a sequential order and allows access using element indices. Unlike Java arrays, the size of an ArrayList is not fixed, enabling the addition of elements without adjusting the memory size. If the Java array contains primitive data types, these should be boxed into their corresponding wrapper data types before conversion to an ArrayList, as ArrayLists can only store Java objects. For non-primitive data types, the Arrays
class in Java provides support for converting the array to an ArrayList.
package com.test; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class JavaArray { public static void main(String[] args) { Integer[] values = new Integer[] { 1, 2, 3 }; List list = Arrays.asList(values); System.out.println(list); int[] values2 = new int[] { 1, 2, 3 }; List list2 = Arrays.stream(values2).boxed().collect(Collectors.toList()); System.out.println(list2); } }
Java Array from List
Converting a Java List, such as an ArrayList, to a Java Array is possible using the toArray
method. An ArrayList is a collection of objects with the same data type, containing Java objects. The toArray
method in the List interface allows the conversion of an ArrayList to a Java Array. If the ArrayList holds objects, this method can be directly employed for conversion. However, if the ArrayList contains wrapper classes, only a wrapper data type array can be created. To convert it to a primitive data type Java array, iterate through the ArrayList and insert the elements into the new array.
package com.test; import java.util.ArrayList; import java.util.List; public class JavaArray { public static void main(String[] args) { List list = new ArrayList(); list.add(1); list.add(2); list.add(3); Integer[] values = list.toArray(new Integer[0]); int[] values2 = new int[list.size()]; for(int i = 0; i < list.size(); i++) values2[i] = list.get(i); Integer[] values3 = list.stream().toArray(Integer[]::new); } }
Java Array to String
Printing the elements of a Java Array directly using the toString
method will output the memory location of the array rather than its elements. To print all the elements, the Arrays
class provides a helpful method for converting the Java Array to a string representation. The example below illustrates how to convert a Java Array to a string containing all of its elements.
package com.test; import java.util.Arrays; public class JavaArray { public static void main(String[] args) { int[] values = {1,2,3}; String str = Arrays.toString(values); System.out.println(str); Integer[] values1 = {1,2,3}; String str1 = Arrays.toString(values1); System.out.println(str1); } }
Java Array from String
When you have a Java string containing multiple strings separated by a delimiter, such as a space or a comma, and you need to convert it into a Java array of strings, you can use the split
method along with the desired delimiter. This method splits the string into an array of substrings based on the specified delimiter. The example below illustrates how to convert a string to a Java array.
package com.test; import java.util.Arrays; public class JavaArray { public static void main(String[] args) { String str = "1,2,3"; String[] values = str.split(","); System.out.println(values.length); int[] values1 = Arrays.stream(str.split(",")).mapToInt(Integer::parseInt).toArray(); System.out.println(values1); } }
Java Array to Stream
Starting with Java 8, you can utilize object streams to simplify the handling and iteration over Java Collections objects. When it comes to processing array elements efficiently, converting a Java array to a Java stream becomes a convenient approach. The example below illustrates how to achieve this conversion.
package com.test; import java.util.Arrays; import java.util.stream.IntStream; import java.util.stream.Stream; public class JavaArray { public static void main(String[] args) { int[] values = {1,2,3}; IntStream stream1 = Arrays.stream(values); stream1.forEach(x -> System.out.println(x)); String[] array = {"1", "2", "3"}; Stream stream2 = Stream.of(array); stream2.forEach(x -> System.out.println(x)); } }