How to Convert Array to List in Java
In this article, we are going to cover in-depth all possible ways to convert an array into a list in Java.
First, we will start by exploring different approaches that were introduced before Java 8. Then, we are going to demonstrate how to achieve the same objective using Java 8 and above.
To conclude, we will look at some external libraries such as Guava and Apache Commons.
If you are looking for the other way around, our article on how to convert a list to array does a great job in covering the topic.
List into Array in Java 7
Java 7 provides several classes and methods to turn an array into a list. Let’s go down the rabbit hole and discover each method in detail.
Using Arrays.asList()
The most easiest and straightforward approach to getting a mutable list from an array of elements is to use Arrays.asList() method.
Let’s see it in action:
public static void usingArraysAsList() {
String[] citiesArray = new String[] { "Tamassint", "New york", "London", "Madrid", "Paris", "Tokyo" };
List<String> citiesList = Arrays.asList(citiesArray);
for (String city : citiesList) {
System.out.println(city);
}
}
The above code will produce the following output:
Tamassint
New york
London
Madrid
Paris
Tokyo
Using Collections.addAll()
Collections.addAll() provides another great approach to creating a list from an array in Java.
Collections is a utility class that consists of a host of ready-to-use static methods to operate collections.
Among these methods, we find addAll(). As the name indicates, it adds the specified elements to the specified collection.
Now, let’s see how we can use it to convert an array to a list:
public static void usingCollectionsAddAll() {
String[] countriesArray = new String[] { "United States", "United Kingdom", "Malaysia", "France", "China", "Germany" };
List<String> countriesList = new ArrayList<>();
Collections.addAll(countriesList, countriesArray);
System.out.println(countriesList);
}
The output will be:
[United States, United Kingdom, Malaysia, France, China, Germany]
Using Collections.unmodifiableList()
Collections comes with another handy method called unmodifiableList(). It returns an immutable list from an array.
Let’s exemplify the use of Collections.unmodifiableList():
public static void usingCollectionsUnmodifiableList() {
String[] frameworkArray = new String[] { "Angular", "Vue", "React", "Flask", "Spring", "Django" };
List<String> frameworkList = Collections.unmodifiableList(Arrays.asList(frameworkArray));
System.out.println(frameworkList);
}
The program will output the following result:
[Angular, Vue, React, Flask, China, Django]
List from Array Using Java 8
Now that we know how to get a list from an array in Java 7, let’s see what features Java 8 brings to the party.
Java 8 provides the stream API that we can use to create a list from an array:
public static void usingJava8() {
String[] animalArray = new String[] { "Cat", "Dog", "Lion", "Horse", "Tiger", "Fox" };
List<String> animalList = Arrays.stream(animalArray)
.collect(Collectors.toList());
System.out.println(animalList);
}
Please bear in my mind that we can convert an array of primitives values to a list using the boxed() method.
Using Java 9
Java 9 comes with rich features called static factory methods to create immutable collections.
Typically, we can create an immutable List from an array by calling the List.of() method:
public static void usingJava9() {
String[] colorArray = { "Red", "Orange", "White", "Blue", "Black", "Green" };
List<String> colorList = List.of(colorArray);
System.out.println(colorList);
}
Using Java 16
Java 16 upgraded the stream API with a brand new feature called toList().
We can use this method to accumulate the elements of an array into a List:
public static void usingJava16() {
int[] ageArray = new int[] { 23, 12, 45, 34, 50 };
List<Integer> ageList = Arrays.stream(ageArray)
.boxed()
.toList();
System.out.println(ageList);
}
Google Guava Library
Alternatively, Guava API offers a convenient way to convert an array of elements into a list through the Lists.newArrayList() method:
public static void usingGuava() {
Double[] doubleArray = { 1.6, 6.8, 3.3, 4.8, 11.5, 89.1 };
List<Double> doubleList = Lists.newArrayList(doubleArray);
}
Please note that we need to add the Guava dependency in pom.xml.
Apache Commons Library
Another solution would be using the Apache Commons library.
So, let’s see how to use the Collections API of the Apache Commons to produce a List from an array:
public static void usingApacheCommonsCollections() {
Integer[] integerArray = { 11, 22, 33, 44, 55, 66 };
List<Integer> integerList = new ArrayList<>(integerArray.length);
CollectionUtils.addAll(integerList, integerArray);
}
Simarily, since it’s an exernal library, we have to add it’s dependency in pom.xml first.
Conclusion
In this article, we investigated multiple ways to convert an array to a list in Java.
First, we illustrated several ways of converting arrays to lists using JDK. Then, we explained how to accomplish the same objective using external libraries.