How do you create a list in Java?

A Java List can be created in multiple ways depending on whether you need a modifiable list, a fixed-size list, or want to initialize it with values in a single statement.

Way 1: Raw Type (Not Recommended)

Create a List without specifying the type of elements. The compiler will show an unchecked warning −

List list = new ArrayList();

Way 2: Generic Type (Recommended)

Create a List with a specified element type for type safety −

List<String> list = new ArrayList<>();

Way 3: Initialize in One Line

Create and initialize the list using Arrays.asList(). Note that this returns a fixed-size list. Wrap it in an ArrayList for a modifiable list −

List<String> fixedList = Arrays.asList("A", "B", "C");
List<String> modifiableList = new ArrayList<>(Arrays.asList("A", "B", "C"));

Example

The following example demonstrates different ways to create a List ?

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CollectionsDemo {
    public static void main(String[] args) {
        // Way 2: Generic ArrayList, add elements individually
        List<String> list = new ArrayList<>();
        list.add("Zara");
        list.add("Mahnaz");
        list.add("Ayan");
        System.out.println("List: " + list);

        // Way 3: Initialize in one line (fixed-size)
        List<String> list1 = Arrays.asList("Zara", "Mahnaz", "Ayan");
        System.out.println("List1: " + list1);

        // Way 3: Initialize in one line (modifiable)
        List<String> list2 = new ArrayList<>(Arrays.asList("Zara", "Mahnaz", "Ayan"));
        list2.add("Rob");
        System.out.println("List2: " + list2);
    }
}

The output of the above code is ?

List: [Zara, Mahnaz, Ayan]
List1: [Zara, Mahnaz, Ayan]
List2: [Zara, Mahnaz, Ayan, Rob]

Conclusion

Use generic ArrayList<>() for modifiable lists with type safety. Use Arrays.asList() for quick fixed-size initialization, or wrap it in new ArrayList<>() if you need to add or remove elements later. For Java 9+, List.of() provides an immutable list alternative.

Updated on: 2026-03-14T17:04:35+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements