Question
I want to create a list of string values for testing purposes in Java. My original code was:
ArrayList<String> places = new ArrayList<String>();
places.add("Buenos Aires");
places.add("Córdoba");
places.add("La Plata");
I then refactored it to:
ArrayList<String> places = new ArrayList<String>(
Arrays.asList("Buenos Aires", "Córdoba", "La Plata")
);
Is there a better or more idiomatic way to initialize an ArrayList in one line?
Short Answer
By the end of this page, you will understand several common ways to initialize lists in Java, especially ArrayList in a single line. You will learn when to use Arrays.asList(...), when to wrap it in new ArrayList<>(...), how modern Java uses List.of(...), and how mutability affects which approach is best.
Concept
In Java, list initialization is really about two separate decisions:
- How to provide the starting values
- Whether the resulting list should be mutable or immutable
This matters because not every list-like object in Java behaves the same way.
For example:
new ArrayList<>()creates a normal, mutable resizable list.Arrays.asList(...)creates a fixed-size list backed by an array.List.of(...)creates an immutable list in modern Java.
Your refactored version is a very common and good approach:
ArrayList<String> places = new ArrayList<>(
Arrays.asList("Buenos Aires", "Córdoba", "La Plata")
);
Why does this work well?
Arrays.asList(...)quickly creates a list from values.new ArrayList<>(...)copies those values into a real mutableArrayList.
This is useful because Arrays.asList(...) alone does not give you a fully flexible ArrayList.
In real programming, this concept matters whenever you:
Mental Model
Think of Java list initialization like packing items into a container.
Arrays.asList(...)is like putting items into a tray with fixed slots. You can replace an item in a slot, but you cannot add more slots or remove slots.new ArrayList<>(...)is like moving those items into a flexible bag that can grow or shrink.List.of(...)is like sealing the items in a display case: you can look at them, but you cannot change them.
So the real question is not just "How do I write this in one line?" It is also: What kind of list behavior do I want afterward?
Syntax and Examples
Common ways to initialize a list in Java
1. Mutable ArrayList with initial values
ArrayList<String> places = new ArrayList<>(
Arrays.asList("Buenos Aires", "Córdoba", "La Plata")
);
Use this when:
- you want a real
ArrayList - you may add or remove items later
Example:
ArrayList<String> places = new ArrayList<>(
Arrays.asList("Buenos Aires", "Córdoba", "La Plata")
);
places.add("Mendoza");
System.out.println(places);
Output:
[Buenos Aires, Córdoba, La Plata, Mendoza]
2. Fixed-size list with Arrays.asList(...)
List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
Use this when:
Step by Step Execution
Consider this example:
List<String> places = new ArrayList<>(
Arrays.asList("Buenos Aires", "Córdoba", "La Plata")
);
Here is what happens step by step:
- Java evaluates
Arrays.asList("Buenos Aires", "Córdoba", "La Plata"). - That creates a list containing three strings.
- That list is passed into the
ArrayListconstructor. - The constructor copies the elements into a new mutable
ArrayList. - The variable
placesnow refers to that newArrayList.
Now trace this code:
List<String> places = new ArrayList<>(
Arrays.asList("Buenos Aires", "Córdoba", "La Plata")
);
places.add("Mendoza");
System.out.println(places);
Execution:
- Start with
[ "Buenos Aires", "Córdoba", "La Plata" ] places.add("Mendoza")appends a new element.- Final list becomes:
Real World Use Cases
This pattern appears often in real Java code.
Test data
List<String> testUsers = new ArrayList<>(Arrays.asList("alice", "bob", "carol"));
Useful for unit tests, mock data, and examples.
Default configuration values
List<String> supportedLocales = List.of("en", "es", "fr");
Good when values should not change.
API input preparation
List<Integer> ids = new ArrayList<>(Arrays.asList(10, 20, 30));
ids.add(40);
Useful when building request payloads dynamically.
Seed data for scripts or demos
List<String> cities = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
Good when you only need to iterate over a fixed set of values.
Real Codebase Usage
In real codebases, developers usually choose the initialization style based on mutability and readability.
Common patterns
Program to the List interface
List<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
This makes it easier to later change the implementation.
Use immutable lists for constants
private static final List<String> SUPPORTED_CITIES = List.of(
"Buenos Aires",
"Córdoba",
"La Plata"
);
This prevents accidental changes.
Copy before modifying external data
List<String> input = Arrays.asList("a", "b", "c");
List<String> editable = new ArrayList<>(input);
editable.add("d");
This is common when a method receives data but needs a mutable copy.
Validation and defensive coding
Developers often use immutable lists where possible because they reduce bugs caused by accidental mutation.
Common Mistakes
1. Assuming Arrays.asList(...) returns a normal ArrayList
Broken example:
List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
places.add("Mendoza");
Problem:
- This compiles.
- It fails at runtime with
UnsupportedOperationException.
Fix:
List<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
places.add("Mendoza");
2. Declaring the variable as ArrayList when List is enough
Less flexible:
ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
Usually better:
Comparisons
| Approach | Java Version | Mutable? | Can add/remove? | Notes |
|---|---|---|---|---|
new ArrayList<>() + add(...) | All | Yes | Yes | Most explicit, more lines |
Arrays.asList(...) | All | Partly | No | Fixed-size list |
new ArrayList<>(Arrays.asList(...)) | All | Yes | Yes | Common one-line mutable solution |
List.of(...) | Java 9+ | No | No |
Cheat Sheet
Quick reference
Mutable ArrayList in one line
List<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));
Fixed-size list
List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
- Can replace values with
set(...) - Cannot
add(...)orremove(...)
Immutable list
List<String> places = List.of("Buenos Aires", "Córdoba", "La Plata");
- Cannot modify at all
- Java 9+
Mutable copy of immutable or fixed-size list
List<String> places = new ArrayList<>(List.of("Buenos Aires", "Córdoba", ));
FAQ
Is Arrays.asList(...) the same as new ArrayList<>(...)?
No. Arrays.asList(...) returns a fixed-size list, while new ArrayList<>(...) creates a normal resizable list.
What is the best one-line way to create a mutable list in Java?
A common choice is:
List<String> items = new ArrayList<>(Arrays.asList("a", "b", "c"));
If you use Java 9+, this is also good:
List<String> items = new ArrayList<>(List.of("a", "b", "c"));
Should I declare the variable as List or ArrayList?
Usually List is better because it keeps your code flexible and less tied to one implementation.
Why does add() fail after using Arrays.asList(...)?
Because the returned list has a fixed size. It supports reading and replacing elements, but not growing or shrinking.
Mini Project
Description
Create a small Java program that builds a list of city names for a test scenario. The project demonstrates the difference between a mutable list and an immutable or fixed-size list, and helps you practice choosing the right initialization style.
Goal
Build and modify a city list using one-line initialization, then print the results to verify which list types can be changed.
Requirements
- Create one list of cities using a one-line mutable
ArrayListinitialization. - Add one extra city to the mutable list.
- Create a second list using
List.of(...)orArrays.asList(...). - Print both lists.
- Show, in code or comments, which list can be modified and which cannot.
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert a Java Stack Trace to a String
Learn how to convert a Java exception stack trace to a string using StringWriter and PrintWriter, with examples and common mistakes.