This article illustrates How to convert an Array and a List in Java. Its examples include plain Java, Guava Library and Apache Commons Collections library.
Tutorial Contents
Converting an Array to a List
Let’s quickly go through the ways to Convert a Java Array to a Java ArrayList.
An Array to List with Plain Java
Java provides the Arrays class that contains various static utility methods to work with Java Arrays. We can use the asList() method of the class to convert a Java Array into a List.
Example of creating an immutable ArrayList from an Array
Integer[] array = new Integer[]{98, 99, 100};
List<Integer> list = Arrays.asList(array);
Code language: Java (java)
The List created by the Arrays.asList(array) is immutable. Thus, we cannot modify it. However, if we modify the underlying Array, the List reflects the change.
If we wish to create a mutable List independent of the Array, we can use an ArrayList constructor.
Example of creating a mutable ArrayList from an Array
Integer[] array = new Integer[]{98, 99, 100}
List<Integer> list =
new ArrayList<>(Arrays.asList(array));
Code language: Java (java)
An Array to List with Apache Commons Library
The Apache Commons Collections (link) is an open-source library containing many useful abstractions and extensions of the Java Collections Framework. Its CollectionUtils class provides an addAll method that can copy elements between two collections or iterables.
Example of using Apache Commons Library to Copy Java Array elements to Java List.
Integer[] array = new Integer[]{98, 99, 100};
List<Integer> list = new ArrayList<>();
CollectionUtils.addAll(list, array);
Code language: Java (java)
First, we created an empty ArrayList using a constructor and used the addAll() method to copy all Array elements in the List.
An Array to List with Google Guava Library
Like the Apache Commons, the Google Guava (link) is an open-source library that provides abstractions over Java Collections Framework.
The Lists class of the library provides the newArrayList() method, a static factory method, to create a new ArrayList containing all the elements of the given Array.
Integer[] array = new Integer[]{98, 99, 100};
List<Integer> list = Lists.newArrayList(array);
Code language: Java (java)
Converting a List to an Array
So far, we have seen various ways to convert a Java Array to a List. This section will see how to convert a Java List into an Array.
A List to an Array with Plain Java
The toArray() method of the Java List interface returns an Object Array (Object[]) containing all the elements within the List.
Object[] toArray();
Code language: Java (java)
However, we can create an Array of a more specific type by providing an empty of the respective type. For example, let’s create an Array of Integers from an Integer List.
List<Integer> list = List.of(50, 51, 52);
Integer[] array = list.toArray(new Integer[0]);
Code language: Java (java)
A List to an Array with Guava Library
We can use the Ints class from Google’s Guava Library to convert a Java Integer List to an Array of Integers.
List<Integer> list = List.of(50, 51, 52);
int[] array = Ints.toArray(list);
Code language: Java (java)
The Ints is Guava’s static utility class to work with int primitives. It also provides similar utility classes for other primitives like Longs, Doubles, Floats, Chars, etc.
Summary
This tutorial covered different ways of converting between a Java List and an Array in Java. The Java API, Guava API and Apache Commons Collections API provide very easy ways for these conversions.
The compelte source code of the examples are available at our GitHub Repository.