How Do I Create a New List in Java?

Abdul D.
—The Problem
You don’t know how to create a new list in Java
The Solution
The most common way to create a new list in Java is to use the ArrayList class from the java.util package, as it offers more flexibility when managing and adding elements than other list types, while still implementing the List interface for abstraction.
However, if you require your list to be immutable, you can use List directly. This is useful if you need your list to be read-only.
Using ArrayList
ArrayListThe ArrayList class implements the List interface. Here’s an example:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> snackList = new ArrayList<>(Arrays.asList("Apple", "Baguette", "Chai")); System.out.println("My List: " + snackList); // Prints My List: [Apple, Baguette, Chai] } }
You can then use methods to manipulate list elements. For example, you can use the add() method to insert another element into the above list:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> snackList = new ArrayList<>(Arrays.asList("Apple", "Baguette", "Chai")); snackList.add("Donut"); System.out.println("My List: " + snackList); // Prints My List: [Apple, Baguette, Chai, Donut] } }
Using List.of()
List.of()In Java version 9 and later, you can create an immutable list using the List.of() method:
import java.util.List; class Main { public static void main(String[] args) { List<String> snackList = List.of("Apple", "Baguette", "Chai"); System.out.println("My List: " + snackList); // Prints My List: [Apple, Baguette, Chai] } }
This creates an immutable list, meaning you cannot add or remove elements after creating it. Attempting to do so will throw an UnsupportedOperationException:
import java.util.List; class Main { public static void main(String[] args) { List<String> snackList = List.of("Apple", "Baguette", "Chai"); // This will throw an error. snackList.add("Donut"); } }
Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:71) at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.add(ImmutableCollections.java:75) at Main.main(Main.java:7)
Choosing Between Different List Creation Methods
When deciding how to create a list, consider the following:
- The
ArrayListmethod is best used when you require flexibility to modify elements. - The
List.of()method is best used when you need a list for constants.
- Sentry BlogException Handling in Java (with Real Examples)
- Syntax.fmListen to the Syntax Podcast
- Listen to the Syntax Podcast
![Syntax.fm logo]()
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODES
Considered “not bad” by 4 million developers and more than 150,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
