How to Initialise a List Inline in Java

A quick guide to Inline lists Java. Inline Lists are created and initialised at the time of creation, in one line.

Create Mutable List (Basic way)

The most basic way of creating and initialising an ArrayList is to do it in multi-line manner.

List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3);
Code language: Java (java)

Create List from Another Collection

Alternatively, we can create a list inline using another collection.

Collection<Integer> hashSet = Set.of(1, 2, 3); List<Integer> list = new ArrayList<>(hashSet);
Code language: Java (java)

First, we are initialising a HashSet instance Inline. Next we are using ArrayList constructor to create a list instance.

Create List using Java 8 Stream Collectors

The Java Streams Collectors supports collecting steam elements into a list.

In the next example, we are creating a stream of three numbers and collecting it to a list using toList method.

List<Integer> list = Stream.of(1, 2, 3) .collect(Collectors.toList());
Code language: Java (java)

Alternatively, we can use toCollection collector by providing a supplier of ArrayList.

In the next example, we are creating a stream of three numbers and collecting them with toCollection collector by providing an ArrayList constructor.

List<Integer> list = Stream.of(1, 2, 3) .collect(Collectors.toCollection(ArrayList::new));
Code language: Java (java)

Using Anonymous Subclass to Create an ArrayList

List<Integer> list = new ArrayList<>() { { add(1); add(2); add(3); } };
Code language: Java (java)

Although this looks simple, we should avoid using anonymous subclass to initialise a list inline. It creates an anonymous subclass as well as an extra initialisation block.

Immutable List using Java Collections

We can create an Immutable List from another mutable or immutable list using unmodifiableList method of Collections class.

List<Integer> list = Collections .unmodifiableList(anotherList);
Code language: Java (java)

Note that the output we get is an immutable list. Thus, we cannot modify it after creation.

Create Singleton List using Collections

A singleton list is a list with one and only one element.

List<Integer> list = Collections.singletonList(1);
Code language: Java (java)

The singletonList method takes the element as an argument and builds an implicitly immutable singleton list.

Create Immutable List using factory Methods

Java 9 introduced factory methods in the List interface which can be used to create lists inline.

List<Integer> list = List.of(1, 2, 3);
Code language: Java (java)

It is important to note that the list returned by the factory methods is immutable.

Create List using Arrays Class

Most commonly used technique of creating lists inline is to use an array. In Java an array can be initialised inline. Thus, we can create an array inline and use it to create a list.

Create List using another array

Integer[] array = new Integer[]{1, 2, 3}; List<Integer> list = Arrays.asList(array);
Code language: Java (java)

Here, we are creating an array and using it with Arrays.asList method to get a List of integers.

When we create a list in this way, the original array and the list refer to the same elements. This is why the list we get is unmodifiable. However, if we make changes to original array those will be reflected in the list as well.

array[0] = 100;
Code language: Java (java)

For example, we modified the first element in the array. Next, we will print both the list and the array and confirm that both of them are modified.

List -> [100, 2, 3]
Array -> [100, 2, 3]

Create List using Variable Arguments

The asList method on the Arrays class takes varargs. Hence, instead of creating an array, we can pass the elements as method arguments.

List<Integer> list = Arrays.asList(1, 2, 3);
Code language: Java (java)


In this tutorial on Creating and Initialising Java Lists Inline, we covered various way of creating mutable, immutable, and singleton lists using Java.

Further Reading:

How to Initialise a HashSet Inline in Java
How to Initialise a HashMap Inline in Java