Initializing a List in Java

Last Updated : 26 Aug, 2026

A List in Java is an ordered collection that allows duplicate elements. It is part of the java.util package and is implemented by classes such as ArrayList, LinkedList, Vector, and Stack. There are several ways to initialize a List depending on whether you need an empty list, mutable list, fixed-size list, or unmodifiable list.

  • Arrays.asList() creates a fixed-size List backed by an array.
  • List.of() creates an unmodifiable List and was introduced in Java 9.

Examples:

Input: List<String> fruits = new ArrayList<>();
Output: [Apple, Banana, Mango]

Input: List<String> languages = List.of("Java", "Python", "C++");
Output: [Java, Python, C++]

Algorithm

  • Import the required classes from java.util.
  • Declare a List using the List interface.
  • Choose an implementation or initialization method based on the requirement.
  • Add elements using add() for mutable Lists.
  • Access elements using get() and the index.

Different Approaches to Initialize a List

1. Using ArrayList

The most common way to create a mutable List is by using the ArrayList class. Elements can be added or removed after initialization.

Java
import java.util.ArrayList;
import java.util.List;

public class Geeks {
    public static void main(String[] args) {

        // Create a mutable List
        List<String> fruits = new ArrayList<>();

        // Add elements
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Mango");

        System.out.println("Fruits: " + fruits);
    }
}

Output
Fruits: [Apple, Banana, Mango]

Explanation: The ArrayList creates a mutable List. The add() method is used to insert elements, and the List maintains their insertion order.

2. Using Arrays.asList()

The Arrays.asList() method can be used to create a List with predefined elements. The resulting List is fixed-size, so elements cannot be added or removed, but existing elements can be replaced using set().

Java
import java.util.Arrays;
import java.util.List;

public class Geeks {
    public static void main(String[] args) {

        List<String> colors =
            Arrays.asList("Red", "Green", "Blue");

        System.out.println("Colors: " + colors);
    }
}

Output
Colors: [Red, Green, Blue]

Explanation: Arrays.asList() creates a fixed-size List containing the specified elements. Calling add() or remove() on this List throws UnsupportedOperationException.

3. Using List.of()

Java 9 introduced the List.of() method to create an unmodifiable List with predefined elements.

Java
import java.util.List;

public class Geeks {
    public static void main(String[] args) {

        List<String> languages =
            List.of("Java", "Python", "C++");

        System.out.println("Languages: " + languages);
    }
}

Output
Languages: [Java, Python, C++]

Explanation: List.of() creates an unmodifiable List. Elements cannot be added, removed, or replaced. It also does not allow null elements.

4. Using ArrayList with List.of()

If we want to initialize a mutable List with predefined values, we can pass a List created using List.of() to the ArrayList constructor.

Java
import java.util.ArrayList;
import java.util.List;

public class Geeks {
    public static void main(String[] args) {

        List<String> animals =
            new ArrayList<>(List.of("Dog", "Cat", "Elephant"));

        animals.add("Lion");

        System.out.println("Animals: " + animals);
    }
}

Output
Animals: [Dog, Cat, Elephant, Lion]

Explanation: List.of() initially provides the elements, while new ArrayList<>(...) creates a separate mutable List. Therefore, elements can be added or removed afterward.

5. Using Java Streams

Java Streams can also be used to create a List from a sequence of elements. With Java 16 and later, the toList() method can be used directly.

Java
import java.util.List;
import java.util.stream.IntStream;

public class Geeks {
    public static void main(String[] args) {

        List<Integer> numbers =
            IntStream.rangeClosed(1, 5)
                     .boxed()
                     .toList();

        System.out.println("Numbers: " + numbers);
    }
}

Output
Numbers: [1, 2, 3, 4, 5]

Explanation: IntStream.rangeClosed() generates integers from 1 to 5. boxed() converts the primitive int values into Integer objects, and toList() collects them into an unmodifiable List.

Note: Stream.toList() was introduced in Java 16. In Java 8, use Collectors.toList() instead.

6. Using Collections.emptyList()

Collections.emptyList() can be used when an empty, unmodifiable List is required.

Java
import java.util.Collections;
import java.util.List;

public class Geeks {
    public static void main(String[] args) {

        List<String> list = Collections.emptyList();

        System.out.println("List: " + list);
    }
}

Output
List: []

Explanation: Collections.emptyList() returns an empty unmodifiable List. If elements need to be added later, use new ArrayList<>() instead.

Comment