Let's get started with a Microservice Architecture with Spring Cloud:
List vs. ArrayList in Java
Last updated: December 9, 2025
1. Overview
In this article, we’ll look into the differences between using the List and ArrayList types.
First, we’ll see a sample implementation using ArrayList. Then, we’ll switch to the List interface and compare the differences.
2. Using ArrayList
ArrayList is one of the most commonly used List implementations in Java. It’s built on top of an array, which can dynamically grow and shrink as we add/remove elements. It’s good to initialize a list with an initial capacity when we know that it will get large:
ArrayList<String> list = new ArrayList<>(25);
By using ArrayList as a reference type, we can use methods in the ArrayList API that are not in the List API — for example, ensureCapacity, trimToSize, or removeRange.
2.1. Quick Example
Let’s write a basic passenger processing application:
public class ArrayListDemo {
private ArrayList<Passenger> passengers = new ArrayList<>(20);
public ArrayList<Passenger> addPassenger(Passenger passenger) {
passengers.add(passenger);
return passengers;
}
public ArrayList<Passenger> getPassengersBySource(String source) {
return new ArrayList<Passenger>(passengers.stream()
.filter(it -> it.getSource().equals(source))
.collect(Collectors.toList()));
}
// Few other functions to remove passenger, get by destination, ...
}
Here, we’ve used the ArrayList type to store and return the list of passengers. Since the maximum number of passengers is 20, the initial capacity for the list is set to this.
2.2. The Problem with Variable Sized Data
The above implementation works fine so long as we don’t need to change the type of List we’re using. In our example, we chose ArrayList and felt it met our needs.
However, let’s assume that as the application matures, it becomes clear that the number of passengers varies quite a lot. For instance, if there are only five booked passengers, with an initial capacity of 20, the memory wastage is 75%. Let’s say we decide to switch to a more memory-efficient List.
2.3. Changing the Implementation Type
Java provides another List implementation called LinkedList to store variable-sized data. LinkedList uses a collection of linked nodes to store and retrieve elements. What if we decided to change the base implementation from ArrayList to LinkedList:
private LinkedList<Passenger> passengers = new LinkedList<>();
This change affects more parts of the application because all the functions in the demo application expect to work with the ArrayList type.
3. Switching to List
Let’s see how can we handle this situation by using the List interface type:
private List<Passenger> passengers = new ArrayList<>(20);
Here, we’re using the List interface as the reference type instead of the more specific ArrayList type. We can apply the same principle to all the function calls and return types. For example:
public List<Passenger> getPassengersBySource(String source) {
return passengers.stream()
.filter(it -> it.getSource().equals(source))
.collect(Collectors.toList());
}
Now, let’s consider the same problem statement and change the base implementation to the LinkedList type. Both the ArrayList and LinkedList classes are implementations of the List interface. So, we can now safely change the base implementation without creating any disturbances to other parts of the application. The class still compiles and works fine as before.
4. Handy Utility Methods to Obtain a List
If we use a concrete list type throughout the program, then all of our code is coupled with that list type unnecessarily. This makes it harder to change list types in the future.
In addition, the utility classes available in Java return the abstract type rather than the concrete type. Next, let’s see a few of them.
4.1. Collections.singletonList()
When we need a List containing exactly one element, Java provides a handy utility method: Collections.singletonList(). We don’t have to create a new ArrayList and add an item manually. Instead, we can quickly wrap a single object into an immutable List. This is especially useful when we want to pass a one‑element list to APIs that expect a List.
Next, let’s look at an example:
List<Passenger> singletonList = Collections.singletonList(passenger1);
assertThat(singletonList).hasSize(1);
assertThat(singletonList.get(0)).isEqualTo(passenger1);
assertThrows(UnsupportedOperationException.class, () -> singletonList.add(passenger2));
As we can see, Collections.singletonList() is pretty handy to create a one-element immutable List.
4.2. Collections.unmodifiablelist()
Sometimes we want to share a List with other parts of our code, but ensure that no one can modify it. For this, we can wrap an existing List with Collections.unmodifiableList(). What we get is a read-only view of the original List. We can still read elements, but any attempt to add, remove, or update will throw an exception. This is useful when we want to protect data integrity while still allowing access.
List<Passenger> originalList = new ArrayList<>();
originalList.add(passenger1);
originalList.add(passenger2);
List<Passenger> unmodifiableList = Collections.unmodifiableList(originalList);
assertThat(unmodifiableList).isEqualTo(originalList);
assertThrows(UnsupportedOperationException.class, () -> unmodifiableList.add(passenger3));
assertThrows(UnsupportedOperationException.class, () -> unmodifiableList.remove(passenger2));
So, this approach is convenient for creating a read-only view of a List. It’s worth mentioning that since this is a read-only view of the original List, it reflects the changes made to the underlying List:
originalList.add(passenger3);
assertThat(unmodifiableList).hasSize(3);
As we can see, the unmodifiableList view reflects the changes we made to orginalList,
4.3. Arrays.asList()
When we want to convert an array into a List quickly, we can use Arrays.asList(). This method wraps the array into a fixed-size List backed by the original array. We can read and update elements through the list, but we cannot change its size (no adding or removing).
Next, let’s understand its usage through an example:
Passenger[] array = { passenger1, passenger2 };
List<Passenger> list = Arrays.asList(array);
assertThat(list).hasSize(2);
assertThat(list.get(0)).isEqualTo(passenger1);
assertThat(list.get(1)).isEqualTo(passenger2);
// We can update elements (reflected in the array too)
list.set(1, passenger3);
assertThat(array[1]).isEqualTo(passenger3);
// Verify immutability of size: adding/removing throws UnsupportedOperationException
assertThrows(UnsupportedOperationException.class, () -> list.add(passenger2));
assertThrows(UnsupportedOperationException.class, () -> list.remove(passenger1));
As the example shows, Arrays.asList() makes it a convenient way to work with arrays using the List API.
4.4. Arrays.subList()
Sometimes we want to work with just a portion of a List, we can use the subList(fromIndex, toIndex) method. It gives us a view of the original List between the specified indices. We can read and modify elements in the sublist, and those changes are reflected in the original List:
List<Passenger> originalList = Arrays.asList(passenger1, passenger2, passenger3, passenger4, passenger5);
List<Passenger> subList = originalList.subList(1, 3);
assertThat(subList).isEqualTo(Arrays.asList(passenger2, passenger3));
// Changes in sublist reflect in the original list
subList.set(1, passenger6);
assertThat(originalList.get(2)).isEqualTo(passenger6);
// immutability of size: adding/removing throws UnsupportedOperationException
assertThrows(UnsupportedOperationException.class, () -> subList.add(passenger4));
assertThrows(UnsupportedOperationException.class, () -> subList.remove(passenger2));
As we can see, we cannot perform add or remove operations on subList.
4.5. List.of()
Starting with Java 9, we can easily create immutable Lists using List.of(). Unlike Arrays.asList(), which produces a fixed-size list backed by an array, List.of() creates a truly unmodifiable List. We can’t add, remove, or change elements, and it doesn’t reflect changes from any underlying array.
Next, let’s see how to use this method by an example:
List<Passenger> list = List.of(passenger1, passenger2, passenger3);
assertThat(list).isEqualTo(Arrays.asList(passenger1, passenger2, passenger3));
assertThrows(UnsupportedOperationException.class, () -> list.add(passenger4));
assertThrows(UnsupportedOperationException.class, () -> list.set(0, passenger4));
assertThrows(UnsupportedOperationException.class, () -> list.remove(passenger2));
List.of() makes it perfect when we want a concise, safe way to define constant lists.
4.6. List.copyOf()
Introduced in Java 10, List.copyOf() allows us to create an immutable copy of an existing collection. Unlike Collections.unmodifiableList(), which provides a read-only view that still reflects changes to the original List, List.copyOf() produces a true snapshot that cannot be modified and remains unchanged even if the source List changes.
As always, let’s understand this through examples:
List<Passenger> originalList = new ArrayList<>();
originalList.add(passenger1);
originalList.add(passenger2);
originalList.add(passenger3);
List<Passenger> copy = List.copyOf(originalList);
assertThat(copy).isEqualTo(originalList);
assertThrows(UnsupportedOperationException.class, () -> copy.add(passenger4));
assertThrows(UnsupportedOperationException.class, () -> copy.set(0, passenger4));
assertThrows(UnsupportedOperationException.class, () -> copy.remove(passenger2));
// Changes to the original list do NOT affect the copy
originalList.add(passenger6);
assertThat(copy).hasSize(3);
As we can see, List.copyOf() is ideal when we want a safe, independent copy of data.
5. Conclusion
In this article, we examined the differences and best practices of using List vs ArrayList types.
We saw how referencing a specific type can make the application vulnerable to change at a later point in time. Specifically, when the underlying implementation changes, it affects other layers of the application. Hence, using the most abstract type (top-level class/interface) is often preferred over using a specific reference type.
Also, we explored some convenient utility methods that return an abstract List type provided by the Java standard library.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
















