Introduction
In this tutorial, we'll be converting an array into a more versatile ArrayList in Java.
- Arrays.asList()
- new ArrayList<>(Arrays.asList()) (Most popular and used approach)
- new ArrayList<>(List.of())
- Collections.addAll()
- Collectors.toList()
- Collectors.toCollection()
- Lists.newArrayList()
Arrays are simple and provide the basic functionality of grouping together a collection of objects or primitive data types. However, arrays are also limited - their size is fixed and even basic operations like adding new items at the beginning or rearranging elements can get complicated.
Thankfully, the Collections Framework introduced us to many very useful implementations of List
s, Set
s, and Queue
s.
One of these is the ArrayList
, a really versatile and popular implementation of a List
.
An ArrayList
's constructor will accept any Collection
. We can get creative with the type of collection we pass into it.
Arrays.asList()
Let's start off with the simplest form of conversion. The Arrays
helper class has a lot of useful methods. The asList()
method returns the contents of the array in a List
:
Employee emp1 = new Employee("John");
Employee emp2 = new Employee("Sarah");
Employee emp3 = new Employee("Lily");
Employee[] array = new Employee[]{emp1, emp2, emp3};
List<Employee> list = Arrays.asList(array);
System.out.println(list);
This will result in a List
implementation (ArrayList
) to be populated with emp1
, emp2
and emp3
. Running this code results in:
[Employee{name='John'}, Employee{name='Sarah'}, Employee{name='Lily'}]
new ArrayList<>(Arrays.asList())
A better approach than just assigning the return value of the helper method is to pass the return value into a new ArrayList<>()
. This is the standard approach used by most people.
This is because the asList()
method is backed by the original array.
If you change the original array, the list will change as well. Also,
asList()
returns a fixed size, since it's backed by the fixed array. Operations that would expand or shrink the list would return aUnsupportedOperationException
.
To avoid these, we'll apply the features of an ArrayList
by passing the returned value of asList()
to the constructor:
Employee[] array = new Employee[]{emp1, emp2, emp3};
List<Employee> list = new ArrayList<>(Arrays.asList(array));
System.out.println(list);
This results in:
[Employee{name='John'}, Employee{name='Sarah'}, Employee{name='Lily'}]
new ArrayList<>(List.of())
Since Java 9, you can skip initializing an array itself and passing it down into the constructor. You can use List.of()
and pass individual elements:
List<Employee> list = new ArrayList<>(List.of(emp1, emp2, emp3));
System.out.println(list);
This results in:
[Employee{name='John'}, Employee{name='Sarah'}, Employee{name='Lily'}]
Collections.addAll()
The Collections
class offers a myriad of useful helper methods and amongst them is the addAll()
method. It accepts a Collection
and a vararg of elements
and joins them up.
It's very versatile and can be used with many collection/vararg flavors. We're using an ArrayList
and an array:
Employee[] array = new Employee[]{emp1, emp2, emp3};
List<Employee> list = new ArrayList<>();
Collections.addAll(list, array);
System.out.println(list);
This results in:
[Employee{name='John'}, Employee{name='Sarah'}, Employee{name='Lily'}]
Collectors.toList()
If you're working with streams, rather than regular collections, you can collect the elements of the stream and pack them into a list via toList()
:
Employee[] array = new Employee[]{emp1, emp2, emp3};
List<Employee> list = Stream.of(array).collect(Collectors.toList());
System.out.println(list);
Running this will yield:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
[Employee{name='John'}, Employee{name='Sarah'}, Employee{name='Lily'}]
Collectors.toCollection()
Similarly, you can use the toCollection()
method to collect streams into different collections. In our case, we'll supply the ArrayList::new
method reference into it, though you could supply other references as well:
Employee[] array = new Employee[]{emp1, emp2, emp3};
List<Employee> list = Stream.of(array)
.collect(Collectors.toCollection(ArrayList::new));
System.out.println(list);
This also results in:
[Employee{name='John'}, Employee{name='Sarah'}, Employee{name='Lily'}]
Lists.newArrayList()
Similar to the Arrays.asList()
helper class and method, Google's Guava project introduced us to the Lists
helper class. The Lists
helper class provides the newArrayList()
method:
Employee[] array = new Employee[]{emp1, emp2, emp3};
List<Employee> list = Lists.newArrayList(array);
Now, the key takeaway of this approach was that you don't need to specify the type when initializing an ArrayList
. This was really useful when you'd have a <Element <Element, Element>>
list.
However, as Java 7 removed the need to explicitly set the type in the diamond operator, this became obsolete.
Conclusion
There are numerous ways to convert an array to an ArrayList in Java. These span from calling helper methods to streaming the array and collecting the elements.