A guide containing examples of creating and initializing Java Lists inline.
Tutorial Contents
Creating a Simple Java ArrayList
At the very basic, we can create an empty Java List using a constructor and add elements.
Example of creating an empty Java List and adding elements to it.
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
Code language: Java (java)
Creating an ArrayList using a Collection
Alternatively, we can initialize an ArrayList inline using another collection in the ArrayList constructor. The constructor creates a mutable instance of ArrayList containing all the elements from the given collection.
Collection<Integer> hashSet = Set.of(1, 2, 3);
List<Integer> list = new ArrayList<>(hashSet);
Code language: Java (java)
First, we initialized a HashSet instance in Inline and used it in the ArrayList constructor.
Creating an ArrayList using Stream Collectors
The Stream interface offers the toList() method, which is an abstraction to collect the Stream elements in the form of an immutable Java List. The toList() method returns a new List containing all Stream elements.
Example of Stream toList() to create a new List containing Stream elements.
List<Integer> list = Stream.of(1, 2, 3).toList();
Code language: Java (java)
Alternatively, we can use toCollection() method of the Collectors class to provide a List instance. The toCollection() collects all Stream elements into the given List and returns it.
List<Integer> list = Stream.of(1, 2, 3)
.collect(Collectors.toCollection(ArrayList::new));
Code language: Java (java)
We used an ArrayList constructor reference to provide an empty ArrayList to collect the elements.
Creating an ArrayList using Anonymous Subclass
Another way to create and initialize an ArrayList inline is to use an anonymous subclass.
List<Integer> list = new ArrayList<>() {
{
add(1);
add(2);
add(3);
}
};
Code language: Java (java)
Although this looks simple, we should avoid using it as it has a lot of overhead and potential memory leak issues.
Create an Immutable ArrayList using Collections
The Java Collections class provides the unmodifiableList(), a factory method, to create an immutable ArrayList containing all the elements of the given Collection or List.
Example of creating an immutable ArrayList inline using another Collection.
List<String> list = Collections
.unmodifiableList(anotherList);
Code language: Java (java)
As the resulting List is immutable, we cannot add, remove or modify its elements.
Creating a Singleton ArrayList
The singletonList() method of the Java Collections class provides a factory method to create an immutable ArrayList instance containing the given element.
List<Integer> list = Collections.singletonList(1);
Code language: Java (java)
A singleton ArrayList instance contains one and only one element. As it is immutable, we cannot modify it once it is created.
Creating an Immutable ArrayList using Factory Methods
Java 9 introduced a factory method in the List class that returns an instance of an immutable ArrayList containing the given element. This is the most straightforward way of creating and initiating an ArrayList in the same line.
Example of using List factory method to initialize an immutable ArrayList inline.
List<Integer> list = List.of(1, 2, 3);
Code language: Java (java)
Creating an ArrayList using Arrays Class
The Arrays class in Java supports creating an ArrayList from a Java array. Let’s understand two ways of using the Arrays class to create and initialize a Java ArrayList inline.
Create an ArrayList using Another Array
Let’s create and initialize an immutable ArrayList instance using an existing Java array.
Integer[] array = new Integer[]{1, 2, 3};
List<Integer> list = Arrays.asList(array);
System.out.println(list);
//prints:
//[1, 2, 3]
Code language: Java (java)
Although the ArrayList we create here is immutable, it is just a view on the original Java Array. That means if we modify the Array, we can see the change is reflected in the immutable List.
Let’s modify an element in the Array and print the Array and the List.
array[0] = 100;
System.out.println(Arrays.toString(array));
System.out.println(list);
//prints:
//[100, 2, 3]
//[100, 2, 3]
Code language: Java (java)
As the output shows, changes to the underlying Array are also reflected in the List. However, the List itself is immutable.
Create an ArrayList using Inline Array
Another version of the Java Array‘s asList() method accepts n elements and returns a new ArrayList containing those elements.
List<Integer> list = Arrays.asList(1, 2, 3);
Code language: Java (java)
Please note that the asList() method internally creates an Array of the given elements, and the Array backs the ArrayList it returns.
Summary
This article demonstrated various ways of creating an ArrayList instance and adding elements in the same line. We covered different examples of an inline initialization of Java ArrayList, including using basic Java factory methods, Stream collectors, and using utility classes like Arrays and Collections.
Please refer to our GitHub Repository for the complete source code of the examples.