Initialize an ArrayList in Java
Last Updated : 07 Apr, 2025
ArrayList is a part of the collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though it may be slower than standard arrays, but can be helpful in programs where lots of manipulation in the array is needed.
- ArrayList inherits the AbstractList class and implements the List interface.
- ArrayList is initialized by a size, however, the size can increase if the collection grows or shrink if objects are removed from the collection.
- Java ArrayList allows us to randomly access the list.
- ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases (see this for details).
- ArrayList in Java can be seen as similar to a vector in C++.
The image below demonstrates the Java Collection Framework hierarchy.

Methods
Below are the various methods to initialize an ArrayList in Java
1. Initialization with add()
Syntax:
ArrayList<Type> al= new ArrayList<Type>();
al.add("Geeks");
al.add("for");
al.add("Geeks");
Example 1: This example demonstrates how to create an ArrayList using the add() method.
Java // Java code to illustrate initialization // of ArrayList using add() method import java.util.*; public class Geeks { public static void main(String args[]) { // create a ArrayList String type ArrayList<String> al = new ArrayList<String>(); // Initialize an ArrayList with add() al.add("Geeks"); al.add("for"); al.add("Geeks"); // print ArrayList System.out.println("ArrayList : " + al); } }
OutputArrayList : [Geeks, for, Geeks]
Note: Now, we are going to discuss the shorthand version of this method.
Example 2: This example demonstrates a shortcut way to create and fill an ArrayList with values in one step.
Java // Java code to illustrate initialization // of ArrayList using add() method import java.util.*; public class Geeks { public static void main(String args[]) { // create a ArrayList String type // and Initialize an ArrayList with add() ArrayList<String> al = new ArrayList<String>() { { add("Geeks"); add("for"); add("Geeks"); } }; // print ArrayList System.out.println("ArrayList : " + al); } }
OutputArrayList : [Geeks, for, Geeks]
2. Initialization using asList()
Syntax:
ArrayList<Type> obj = new ArrayList<Type>(
Arrays.asList(Obj A, Obj B, Obj C, ....so on));
Example: This example demonstrates how to create and initalize an ArrayList using the asList() method.
Java // Java code to illustrate initialization // of ArrayList using asList method import java.util.*; public class Geeks { public static void main(String args[]) { // create a ArrayList String type // and Initialize an ArrayList with asList() ArrayList<String> al = new ArrayList<String>( Arrays.asList("Geeks", "for", "Geeks")); // print ArrayList System.out.println("ArrayList : " + al); } }
OutputArrayList : [Geeks, for, Geeks]
3. Initialization using List.of() Method
Syntax:
List<Type> obj = new ArrayList<>(
List.of(Obj A, Obj B, Obj C, ....so on));
Example: This example demonstrates how to create and initalize an ArrayList using the List.of() method.
Java // Java code to illustrate initialization // of ArrayList using List.of() method import java.util.*; public class Geeks { public static void main(String args[]) { // create a ArrayList String type // and Initialize an ArrayList with List.of() List<String> al = new ArrayList<>( List.of("Geeks", "for", "Geeks")); // print ArrayList System.out.println("ArrayList : " + al); } }
OutputArrayList : [Geeks, for, Geeks]
4. Initialization using another Collection
Syntax:
List gfg = new ArrayList(collection);
Example: This example demonstrates how to create a new ArrayList by copying elements from an existing collection.
Java // Java code to illustrate initialization // of ArrayList using another collection import java.util.*; public class Geeks { public static void main(String args[]) { // create another collection List<Integer> l = new ArrayList<>(); l.add(1); l.add(2); l.add(3); l.add(4); l.add(5); // create a ArrayList Integer type // and Initialize an ArrayList with arr List<Integer> l1 = new ArrayList<Integer>(l); // print ArrayList System.out.println("ArrayList : " + l1); } }
OutputArrayList : [1, 2, 3, 4, 5]
5. Initialization using stream() and collect() Methods
Syntax:
ArrayList<Type> listName = Stream.of(element1, element2, ..., elementN).collect(Collectors.toCollection(ArrayList::new));
Example: This example demonstrates how to create an ArrayList from a Stream using Java 8's Stream API and Collectors.
Java // creating arraylist using java8 stream API import java.util.ArrayList; import java.util.stream.Collectors; import java.util.stream.Stream; public class Geeks { public static void main(String args[]) { // create a stream of elements using Stream.of() // method collect the stream elements into an // ArrayList using the collect() method and // Collectors.toCollection() method ArrayList<String> al = Stream.of("Geeks", "For", "Geeks") .collect(Collectors.toCollection( ArrayList::new)); // print the ArrayList System.out.println(al); } }
Output[Geeks, For, Geeks]