Initialization of an ArrayList in one line
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Java, initializing an ArrayList in one line is easy, but the best syntax depends on whether you want a mutable list, an immutable list, or a quick demo-only shortcut. The most practical answer in modern Java is usually to wrap List.of(...) or Arrays.asList(...) inside new ArrayList<>(...) when you need mutability.
Mutable ArrayList in One Line
If you want a real, resizable ArrayList, this is the common pattern:
This creates a mutable ArrayList populated with the given values. You can still call add, remove, or clear afterward.
For older Java versions, Arrays.asList is the equivalent source list:
Immutable List Versus Mutable ArrayList
If you do not actually need an ArrayList, the simpler answer may be an immutable List.
That is shorter and clearer, but it is not an ArrayList and it cannot be modified.
This distinction matters because many code examples say "ArrayList" when they really mean "a list of values." If the code never mutates the collection, List.of may be the better API.
Why Double-Brace Initialization Is Usually Avoided
You may also see this older pattern:
It works, but it creates an anonymous inner class and has awkward side effects around serialization, class generation, and hidden references. It is generally not recommended for production code.
That is why modern Java code tends to prefer constructor wrapping around List.of or Arrays.asList instead.
Streams Are Possible but Usually Overkill
A stream-based one-liner also works:
This is valid, but it is more verbose than necessary for static literal values. Streams make more sense when the elements are being computed or transformed rather than just listed.
Prefer the Interface When the Concrete Type Does Not Matter
Even when you create an ArrayList, it is often cleaner to declare the variable as List.
This keeps the code flexible. If you later switch to another List implementation, the rest of the code does not need to care as long as it only depends on list behavior.
Practical Decision Rule
Use these rules:
- want a mutable
ArrayList:new ArrayList<>(List.of(...)) - need Java 8 compatibility:
new ArrayList<>(Arrays.asList(...)) - want an immutable list:
List.of(...) - avoid double-brace initialization unless you have a very specific reason
That keeps the code short without introducing unnecessary complexity.
Common Pitfalls
- Using
Arrays.asList(...)directly and then trying to add or remove elements causes runtime errors because the returned list is fixed-size. Wrap it innew ArrayList<>(...)if you need mutability. - Confusing
List.of(...)withArrayListleads to immutable collections where the code expects a resizable list. Choose the type based on whether mutation is required. - Using double-brace initialization for convenience creates an anonymous class and hidden overhead. It is usually not worth the side effects.
- Reaching for streams to build a literal list makes simple code harder to read. Use streams when there is actual transformation logic, not just static values.
- Declaring the variable as
ArrayListwhen onlyListbehavior is needed can make the code more rigid than necessary. Prefer the interface type unless the concrete type matters.
Summary
- The usual one-line mutable
ArrayListpattern isnew ArrayList<>(List.of(...)). - For older Java versions,
new ArrayList<>(Arrays.asList(...))is common. - '
List.of(...)is concise but immutable.' - Double-brace initialization works but is usually discouraged.
- Pick the form based on mutability needs, not just on brevity.

