Open In App

Initializing a List in Java

Last Updated : 11 Oct, 2025
Comments
Improve
Suggest changes
20 Likes
Like
Report

In Java, a List is an ordered Collection that allows duplicate elements. It is part of Java.util.list package and implemented by ArrayList, LinkedList, Vector and Stack 

There are multiple ways to declare and initialize a List depending on your needs, whether you want a fixed-size list, a mutable list, or a pre-filled list.

Java
import java.util.*;

public class Geeks{
    public static void main(String[] args){
        
        // Using ArrayList
        List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Mango");

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

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

        // Accessing elements
        System.out.println("Fruits: " + fruits);
        System.out.println("Colors: " + colors);
        System.out.println("Languages: " + languages);
        System.out.println("First fruit: " + fruits.get(0));
        System.out.println("Number of colors: " + colors.size());
    }
}

Output
Fruits: [Apple, Banana, Mango]
Colors: [Red, Green, Blue]
Languages: [Java, Python, C++]
First fruit: Apple
Number of colors: 3

Different Instances of List

List is an interface, and the instances of List can be created in the following ways:

List a = new ArrayList();
List b = new LinkedList();
List c = new Vector();
List d = new Stack();

1. Importing the List Interface

Before using a List, you need to import it from Java.util package:

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

2. Declaring a List

You can declare a List using the interface type. This only declares the reference variable the memory is allocated once you initialize it with an implementation like ArrayList or LinkedList

List<String> list;

3. Ways to Initialize a List in Java

1. Using ArrayList Constructor

The most common way to create and initialize a List is by using the ArrayList class.

List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");

2. Using Arrays.asList()

We can quickly initialize a List with a set of predefined values using Arrays.asList().

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

3. Creating Mutable List

If we want to create the mutable List. We can use the syntax mentioned below using Arrays.asList() method.

List<Integer> list=new ArrayList<>(Arrays.asList(1, 2, 3));

4. Using Java 9 List.of()

Java 9 introduced List.of() method which takes in any number of arguments and constructs a compact and unmodifiable list out of them. 

List<Integer> unmodifiableList = List.of(1, 2, 3);

5. Using Java 8 Stream

With the introduction of Stream and functional programming in Java 8, now one can construct any stream of objects and then collect them as a list. 

Syntax:

List<Integer> numbers = java.util.stream.IntStream.rangeClosed(1, 5)

.boxed()

.toList(); // Java 16+

You can use double braces to initialize lists in one line, but it creates an anonymous class, which may lead to memory leaks

List<String> animals = new ArrayList<>() {{

add("Dog");

add("Cat");

add("Elephant");

}};

4. Accessing List Elements

We can access elements using the get() method, with the index starting from 0.

System.out.println(languages.get(0));


How to Initialize a List in Java

Explore