Java example

How to initialize a list in one line in Java

7 min read Updated Sep 2026 Runs in an isolated runtime
Quick answer

Use List.of("a", "b", "c") (Java 9+). It returns an unmodifiable list that rejects nulls. If you need to add or remove elements later, copy it into an ArrayList: new ArrayList<>(List.of("a", "b", "c")). Sets and maps work the same way with Set.of and Map.of.

Java has no list literal, so "a list with these values" has been answered many ways over the years: Arrays.asList in Java 1.2, streams in Java 8, and since Java 9 the factory methods List.of, Set.of and Map.of, which are the answer for new code. The real decision is not the syntax but whether the collection may change afterwards: the factories give you a fixed, read-only collection, and a copy into ArrayList, HashSet or HashMap gives you one you can grow. This page covers lists first, then sets and maps, a static final map, and the double-brace pattern you should not copy. Each example runs on this page: hit Run, then edit the code and run it again.

1List.of (Java 9+)Recommended

List.of(e1, e2, …) builds a list from its arguments in one expression. The result is unmodifiable: add, remove and set all throw UnsupportedOperationException. It also refuses null elements, so a missing value fails fast where the list is built instead of somewhere far away. That makes it the right default for constants, test data and any list you only read.

Main.java

Output

Prints [Java, Kotlin, Scala], 3 Java true, add failed: UnsupportedOperationException, null rejected: NullPointerException, then [] [42]. Declare the variable as the List interface, not a concrete class, so you can swap the implementation later. To take a read-only snapshot of a list you already have, use List.copyOf(other).

2A list you can add to: new ArrayList<>(List.of(...))

Most "initialize an ArrayList in one line" questions really want a list that starts with some values and keeps growing. Pass the fixed values to the ArrayList copy constructor: it copies the elements into a new, fully mutable list. The diamond <> lets the compiler infer the element type from the variable. For an empty list to fill later, new ArrayList<>() is all you need, and Collections.addAll appends several values in one call.

Main.java

Output

Prints [draft, ship], [68, 75, 82, 90] and [a, b] [a, b, c]. The last line shows that the copy is independent: adding to it leaves the source list alone. On Java 8, where List.of doesn't exist, write new ArrayList<>(Arrays.asList("write", "test")) instead. To join two existing lists this way, see concatenate two lists.

3Arrays.asList and streams

Arrays.asList is the pre-Java 9 one-liner and still everywhere in older code. It is not a copy but a fixed-size view of an array: you can replace elements with set, but add and remove throw, and writes go straight through to the array behind it. When the values are computed rather than listed, build them with a stream. Stream.toList() (Java 16+) returns an unmodifiable list; Collectors.toList() is the Java 8 form, and Collectors.toCollection(ArrayList::new) guarantees an ArrayList.

Main.java

Output

Prints [red, lime, blue], add failed: UnsupportedOperationException, [z, b, c] (the array itself changed), [1, 4, 9, 16, 25], [X, Y] and [ann, bob, cy]. Collectors.toList() happens to return an ArrayList today, but its documentation promises nothing about mutability, so don't rely on adding to it. Unlike List.of, both Arrays.asList and Stream.toList() accept null elements. For turning an existing array into a list, see convert an array to a list.

4Sets and maps: Set.of, Map.of and Map.ofEntries

The same pattern covers the other collections. Set.of and Map.of (Java 9+) build unmodifiable collections that reject nulls, and copying them into new HashSet<>(…) or new HashMap<>(…) makes them mutable. Map.of takes alternating keys and values and has overloads for up to 10 pairs; past that, or whenever the pairs are easier to read one per line, use Map.ofEntries(Map.entry(k, v), …), which takes any number. One thing to know before you print them: the iteration order of Set.of and Map.of is unspecified and can differ from run to run, and HashSet and HashMap promise no order either. The example looks values up by key, or prints a sorted TreeSet or TreeMap copy, so its output is stable.

Main.java

Output

Prints true 5, [a, e, i, o, u], [java, jvm, lts], 443 false, {apples=12, pears=0, plums=7}, {a=2, b=1, c=1}, then duplicate element: x. A repeated value in Set.of or a repeated key in Map.of throws IllegalArgumentException instead of silently keeping one. When you want to keep insertion order, copy into a LinkedHashSet or LinkedHashMap; for sorted order, a TreeSet or TreeMap. To walk a map's entries, see iterate over a map.

5A static final map, and why not double-brace initialization

For a lookup table shared by the whole class, assign Map.of (or Map.ofEntries) to a static final field: it is built once when the class loads and nobody can change it. When the values need a loop or other code, fill a map in a static initializer block and wrap it with Collections.unmodifiableMap. Old answers often suggest double-brace initialization, new HashMap<>() {{ put(…); }}, instead. It looks like a literal, but the outer braces declare an anonymous subclass of HashMap and the inner ones are its instance initializer, so every place you use it adds another class to your program.

Main.java

Output

Prints Not Found, {a=1, b=2, c=3, d=4, e=5} and {one=1, two=2}, then the problem: Main$1 false and [final Main Main$1.this$0]. The double-brace map is an instance of a separate class, and because it was created inside an instance method it keeps a hidden reference, this$0, to the object that built it. Hand that map to a cache or another thread and the whole outer object stays in memory with it; serializing the map tries to serialize the outer object too. It also breaks code that checks getClass() == HashMap.class. Use Map.of, or new HashMap<>(Map.of(…)) when the map must stay mutable.

6Which should you use?

MethodCan change?NullsBest for
List.of(a, b, c)NoRejectedFixed values, the default (Java 9+)
new ArrayList<>(List.of(a, b))FullyAllowed after creationStarting values you will add to
Arrays.asList(a, b, c)set only, fixed sizeAllowedJava 8 code, viewing an existing array
stream.toList()NoAllowedComputed values (Java 16+)
Set.of(…) / Map.of(…)NoRejectedConstant sets and maps, up to 10 map pairs
Map.ofEntries(Map.entry(k, v), …)NoRejectedMaps with more than 10 pairs
new HashMap<>() {{ put(k, v); }}FullyAllowedNothing: extra class, hidden outer reference

Frequently asked questions

How do I initialize an ArrayList with values in one line?

Write List<String> list = new ArrayList<>(List.of("a", "b", "c"));. List.of (Java 9+) holds the values and the ArrayList copy constructor turns them into a list you can add to and remove from. On Java 8, use new ArrayList<>(Arrays.asList("a", "b", "c")). If the list never changes, skip the copy and keep List.of("a", "b", "c").

Why does add() throw UnsupportedOperationException on my list?

The list is unmodifiable or fixed-size. List.of, List.copyOf, Stream.toList() and Collections.unmodifiableList reject every change, and Arrays.asList allows set but not add or remove because it is backed by an array. Copy it into a new ArrayList, new ArrayList<>(list), and change the copy.

What is the difference between List.of and Arrays.asList?

List.of is fully unmodifiable and rejects nulls: even List.of("a", "b").contains(null) throws a NullPointerException. Arrays.asList is a fixed-size view of an array: set works and writes through to the array, add and remove throw, and nulls are allowed, so Arrays.asList("a", null) prints [a, null]. Prefer List.of in new code unless you need nulls or a view of an existing array.

How do I initialize a Map with more than 10 entries?

Map.of only has overloads for up to 10 key-value pairs, so an 11-pair call does not compile (javac reports no suitable method found for of). Use Map.ofEntries(Map.entry("a", 1), Map.entry("b", 2), ...), which takes any number of entries, or fill a HashMap with put calls in a static initializer block.

Why does Map.of or Set.of print in a different order each time?

Their iteration order is deliberately unspecified and is randomized per JVM run, so code cannot come to depend on it. HashMap and HashSet promise no order either. If order matters, copy into a LinkedHashMap or LinkedHashSet to keep insertion order, or into a TreeMap or TreeSet to sort by key.

How do I create a list with the same value repeated n times?

Use Collections.nCopies(n, value): Collections.nCopies(3, "x") is [x, x, x]. It is unmodifiable, so wrap it in new ArrayList<>(Collections.nCopies(4, 0)) if you want a pre-filled list you can change afterwards.

Run it yourself

Open any of these in the full Java editor: tweak, run, and share.

Java playground