The ArrayList is a class in Java that is also known as a resizable array since the size of the ArrayList increases or decreases when the elements are added or removed. The ArrayLists are commonly used because they change their size dynamically according to the requirements of the program. When declaring the ArrayList in Java the first and foremost step involves the declaration of class and the second step is the initialization of the ArrayList.
This write-up will assist you in understanding what an ArrayList is and how to initialize it in Java.
How to Initialize an ArrayList in Java?
The term initialization refers to assigning the values to the specified data structures in Java. Because of its dynamic nature, it gives more access to the elements present in the data structures. The ArrayList in Java belongs to the java.util package and can be initialized by a variety of built-in methods like add() method, list.of() method, asList() method, stream(), and collect() method.
Below is the implementation of different built-in methods to initialize the ArrayList in Java.
Method 1: Initialization of an ArrayList Using the add()
The code below depicts the implementation of the add() method to initialize the ArrayList in Java.
import java.util.*;//Creating a new class
public class InitializeAdd {
public static void main(String args[])
{ //Creating an instance of the ArrayList class
ArrayList<String> a = new ArrayList<String>();
// The ArrayList is initialized with add()
a.add("Programming");
a.add("is fun");
a.add("in Java ");
// print the ArrayList
System.out.println("The ArrayList is: " + a);
}
}
In the above code:
- The package is imported for the declaration of an ArrayList.
- The class “InitializeAdd” is created for the initialization of ArrayList
- The add() method adds the elements to the ArrayList.
- The println() method prints the ArrayList.
Output
The below output depicts that the ArrayList is printed accordingly.

Method 2: Initialization of an ArrayList Using the list.of()
The below code implements the list.of() method that initializes the elements of the ArrayList in Java.
import java.util.*;//Creating a new class
public class InitializeAdd {
public static void main(String args[])
{ //Creating an instance of the ArrayList class
List<String> a = new ArrayList<String>(List.of("Programming", "in", "Java"));
// print the ArrayList
System.out.println("The ArrayList is : " + a);
}
}
In the above code, an Arraylist is initialized using the List.of() method in Java. The output is printed using the println() method of Java.
Output
The output below shows that the Array list is printed.

Method 3: Initialization Using the asList()
The code below depicts that the asList is used to initialize the List.
import java.util.*;
public class InitializeAdd {
public static void main(String args[])
{
List<String> a = new ArrayList<String>(Arrays.asList("Programming", "in", "Java"));
// print the ArrayList
System.out.println("The ArrayList using asList Method is : " + a);
}
}
In the above code the ArrayList is initialized using the .asList() Method and the output is declared using the println() method of Java.
Output
The output below depicts the elements that are “Programming”, “in”, and “Java” declared in the ArrayList are printed.

Method 4: Initialization of an ArrayList Using stream and collect()
The below code block explains the detailed process of initializing an ArrayList using stream() and collect() methods:
import java.util.ArrayList;
import java.util.stream.*;
public class InitializeArray {
public static void main(String args[])
{
//use Stream.of to create a stream of elements and collect() method to collect the elements
ArrayList<String> l= Stream.of("Programming", "in", "Python")
.collect(Collectors.toCollection(ArrayList::new));
//Print the ArrayList
System.out.println("The ArrayList using stream() and collect() Method is :" +l);
}
}
In the above code:
- The required package of ArrayList and stream is imported.
- In the next step, a class is declared as “InitializeArray”.
- The stream() method creates a stream of the elements and the collect() method collects the elements from that stream along with the Collectors.toCollection() method.
- The output of the ArrayList in Java is printed using the println() method.
Output
The output below depicts that the elements “Programming”, “in”, and “Python” of the ArrayList are printed.

The article explains different methods to initialize an array in Java.
Conclusion
The ArrayList in Java is initialized by declaring the object in the first step and then using different built-in methods like the add() method, list.of method, asList method, stream(), and collect() method. This write-up has discussed several methods to initialize a Java ArrayList.