How to make a new List in Java
Categories:
How to Create and Initialize a List in Java

Learn the fundamental ways to create, initialize, and manipulate List
objects in Java, covering ArrayList
, LinkedList
, and immutable lists.
In Java, the List
interface, part of the Java Collections Framework, represents an ordered collection of elements. Unlike arrays, lists are dynamic in size and can grow or shrink as needed. This article will guide you through the various methods of creating and initializing List
objects, focusing on common implementations like ArrayList
and LinkedList
, and also exploring modern approaches for immutable lists.
Understanding the List Interface and Implementations
The List
interface extends Collection
and defines methods for positional access, search, and iteration. It allows duplicate elements and maintains the insertion order. The two most commonly used concrete implementations are ArrayList
and LinkedList
, each with distinct performance characteristics.
ArrayList
: Implemented using a dynamic array. It provides fast random access (getting an element by index) but can be slower for insertions or deletions in the middle of the list, as it may require shifting elements.LinkedList
: Implemented using a doubly-linked list. It offers efficient insertions and deletions anywhere in the list but slower random access, as it must traverse the list from the beginning or end to reach a specific index.
flowchart TD A[List Interface] --> B[ArrayList (Dynamic Array)] A --> C[LinkedList (Doubly Linked List)] B --> D{"Fast Random Access?"} C --> E{"Fast Insert/Delete?"} D -- Yes --> F[ArrayList Advantage] E -- Yes --> G[LinkedList Advantage] D -- No --> G E -- No --> F
Decision flow for choosing between ArrayList and LinkedList.
Creating a New List
You typically declare a List
using the interface type and instantiate it with a concrete class. This adheres to the principle of programming to an interface, not an implementation.
import java.util.ArrayList;
import java.util.List;
public class ListCreation {
public static void main(String[] args) {
// 1. Using ArrayList (most common)
List<String> names = new ArrayList<>();
System.out.println("Empty ArrayList: " + names);
// 2. Using LinkedList
List<Integer> numbers = new java.util.LinkedList<>();
System.out.println("Empty LinkedList: " + numbers);
// 3. Specifying initial capacity for ArrayList (optimization)
List<Double> grades = new ArrayList<>(10); // Capacity for 10 elements
System.out.println("Empty ArrayList with capacity: " + grades);
}
}
Basic creation of ArrayList
and LinkedList
.
List
interface (List<Type> mylist = new ArrayList<>();
) rather than the concrete implementation (ArrayList<Type> mylist = new ArrayList<>();
). This makes your code more flexible and easier to change if you decide to switch implementations later.Initializing a List with Elements
There are several ways to initialize a List
with elements, ranging from adding them one by one to using utility methods for bulk initialization.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ListInitialization {
public static void main(String[] args) {
// 1. Add elements one by one
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
System.out.println("Fruits (add one by one): " + fruits);
// 2. Using Arrays.asList() (creates a fixed-size list backed by an array)
List<String> colors = Arrays.asList("Red", "Green", "Blue");
// Note: This list is fixed-size. Adding/removing elements will throw UnsupportedOperationException.
System.out.println("Colors (Arrays.asList): " + colors);
// To make it modifiable, wrap it in a new ArrayList
List<String> modifiableColors = new ArrayList<>(Arrays.asList("Red", "Green", "Blue"));
modifiableColors.add("Yellow");
System.out.println("Modifiable Colors: " + modifiableColors);
// 3. Using Collections.addAll()
List<Integer> ages = new ArrayList<>();
Collections.addAll(ages, 25, 30, 35, 40);
System.out.println("Ages (Collections.addAll): " + ages);
// 4. Using Java 9+ List.of() for immutable lists
List<String> immutablePlanets = List.of("Mars", "Jupiter", "Saturn");
// immutablePlanets.add("Earth"); // Throws UnsupportedOperationException
System.out.println("Immutable Planets (List.of): " + immutablePlanets);
// 5. Using Stream API (Java 8+)
List<String> animals = Stream.of("Dog", "Cat", "Bird")
.collect(Collectors.toList());
System.out.println("Animals (Stream API): " + animals);
// 6. Double Brace Initialization (discouraged due to anonymous inner class overhead)
List<String> cities = new ArrayList<String>() {{
add("New York");
add("London");
add("Paris");
}};
System.out.println("Cities (Double Brace Init): " + cities);
}
}
Various methods for initializing Java Lists with elements.
Arrays.asList()
directly. The list it returns is a fixed-size wrapper around the original array. Attempts to add or remove elements will result in an UnsupportedOperationException
. If you need a modifiable list, pass the result of Arrays.asList()
to a new ArrayList
constructor.Choosing the Right List Type
The choice between ArrayList
and LinkedList
depends heavily on the operations you'll perform most frequently:
ArrayList
is generally preferred if you need frequent random access to elements (e.g.,get(index)
) and fewer insertions/deletions in the middle of the list. It's also more memory-efficient for storing primitive types (via their wrapper classes) due to its contiguous memory allocation.LinkedList
is better if you frequently add or remove elements from the beginning or middle of the list, as these operations are constant time (O(1)) once the position is found. However, random access is O(n).
For creating small, fixed-content lists, especially in Java 9+, List.of()
is an excellent choice for its conciseness and immutability, which can prevent accidental modifications and improve thread safety.
1. Step 1: Import necessary classes
Ensure you import java.util.List
and the specific implementation you plan to use, such as java.util.ArrayList
or java.util.LinkedList
.
2. Step 2: Declare the List
Declare your list using the List
interface: List<DataType> myListName;
3. Step 3: Instantiate the List
Instantiate your chosen implementation: myListName = new ArrayList<>();
or myListName = new LinkedList<>();
4. Step 4: Initialize with elements (optional)
Add elements using add()
, Collections.addAll()
, Arrays.asList()
(wrapped in new ArrayList<>()
), or List.of()
for immutable lists.