An array stores a fixed number of elements, while a collection such as a List provides methods for storing and manipulating a group of objects. Java provides several ways to convert an array into a collection.
- Arrays.asList() provides a simple way to convert an object array into a fixed-size list.
- Other approaches such as Collections.addAll(), List.of(), and Arrays.stream() can be used when a separate or mutable list is required.
Methods to Convert an Array to a Collection
Following are the commonly used methods to convert an array into a collection in Java.
1. Using Arrays.asList()
The Arrays.asList() method converts an array into a fixed-size List. The returned list is backed by the original array, so changes to elements in the list are reflected in the array.
Syntax:
Arrays.asList(array)
import java.util.*;
class ArrayToCollection {
public static void main(String[] args)
{
String[] players = { "Dhoni", "Kohli", "Rohit", "Bumrah" };
System.out.println("Array: " + Arrays.toString(players));
// Convert array to List
List<String> playerList = Arrays.asList(players);
System.out.println("Collection: " + playerList);
}
}
Output
Array: [Dhoni, Kohli, Rohit, Bumrah] Collection: [Dhoni, Kohli, Rohit, Bumrah]
Explanation: Arrays.asList() converts an array into a List containing the same elements. The returned list is backed by the original array, so changes to existing elements are reflected in the array. However, the size of the list cannot be changed, so add() and remove() operations are not supported.
Note: The list returned by Arrays.asList() has a fixed size. Operations such as add() and remove() are not supported.
2. Using Collections.addAll()
The Collections.addAll() method adds all elements of an array to an existing collection. It is useful when we want to create a separate, mutable ArrayList.
Syntax:
Collections.addAll(collection, array)
import java.util.*;
class ArrayToCollection {
public static void main(String[] args) {
String[] countries = {
"India", "Japan", "Brazil", "Canada"
};
// Create a mutable collection
List<String> countryList = new ArrayList<>();
// Add all array elements to the list
Collections.addAll(countryList, countries);
System.out.println("Array: "
+ Arrays.toString(countries));
System.out.println("Collection: "
+ countryList);
}
}
Output
Array: [India, Japan, Brazil, Canada] Collection: [India, Japan, Brazil, Canada]
Explanation: Collections.addAll() adds all the elements of an array to an existing collection. In this approach, an ArrayList is created first and the array elements are added to it. Since the result is an ArrayList, elements can be added or removed after the conversion.
3. Using List.of()
The List.of() method can be used to create a list containing the elements of an array. It was introduced in Java 9. If a mutable list is required, the result can be passed to the ArrayList constructor.
import java.util.*;
class ArrayToCollection {
public static void main(String[] args) {
String[] countries = {"India", "France", "Germany", "Australia" };
// Convert array to a mutable ArrayList
List<String> countryList = new ArrayList<>(List.of(countries));
System.out.println("Array: " + Arrays.toString(countries));
System.out.println("Collection: " + countryList);
}
}
Output
Array: [India, France, Germany, Australia] Collection: [India, France, Germany, Australia]
Explanation: List.of() creates a list containing all the elements of the given array. The list returned by List.of() is unmodifiable, so its elements cannot be added, removed, or replaced. If a mutable collection is required, it can be passed to the ArrayList constructor.
Note:
List.of()itself returns an unmodifiable list. Wrapping it withnew ArrayList<>(...)creates a mutable list.
4. Using Arrays.stream()
The Arrays.stream() method creates a stream from the array. The stream can then be collected into a List.
import java.util.*;
import java.util.stream.*;
class ArrayToCollection {
public static void main(String[] args) {
String[] subjects = {
"Java", "Python", "C++", "JavaScript"
};
List<String> subjectList = Arrays.stream(subjects).collect(Collectors.toList());
System.out.println("Array: " + Arrays.toString(subjects));
System.out.println("Collection: " + subjectList);
}
}
Output
Array: [Java, Python, C++, JavaScript] Collection: [Java, Python, C++, JavaScript]
Explanation: Arrays.stream() creates a stream from the elements of an array. The stream can then be collected into a List using Collectors.toList(). This approach is especially useful when other Stream API operations, such as filter(), map(), or sorted(), are also required during the conversion.