How to Create and Initialize ArrayList in Java

Last updated on June 7th, 2024

In this topic, we will learn what is ArrayList in Java, how to create an ArrayList object in Java, the constructors of ArrayList and how to initialize ArrayList in Java. ArrayList is a class in Java.

What is ArrayList in Java?

 ArrayList is one of the implementation classes of the List interface in Java. This class is commonly used to create a list in Java by the programmer. This class is imported from java.util package. In this class, the underlying data structure is a resizable array/growable array. This class is stored to allow duplicate objects and heterogeneous objects. In this class, insertion order is preserved. In this class, null insertion is also possible. This class implements Serializable, Cloneable, Iterable, Collection, and RandomAccess interfaces. ArrayList is the best choice for retrieval operation.

How to create an ArrayList object in Java?

We can create an ArrayList object by using new and its constructors.

ArrayList a=new ArrayList();

The constructor of the ArrayList in Java

There are the following constructors of the ArrayList in Java:

ArrayList a=new ArrayList();

→ This creates an empty ArrayList object with a default initial capacity of “10”. If the ArrayList reaches its maximum capacity then a new ArrayList object will be created with this formula below:
New capacity = (current capacity x 3/2)+1

ArrayList a=new ArrayList(int initialcapacity);

→ This creates an empty ArrayList object with the specified initial capacity.

ArrayList a=new ArrayList(Collection c);

→ This creates an equivalent ArrayList object for the given Collection that is this constructor is meant for interconversion between Collection objects.

How to initialize ArrayList in Java?

There are different ways to initialize ArrayList in Java:
    • Initialization with add() method
    • Initialization with Anonymous Inner class
    • Initialization using asList() method
    • Initialization using List.of() method Java 9 feature
    • Initialization using another Collection
    • Initialization using Stream API methods Java 8 feature

Initialization with add() method

We can initialize ArrayList in Java using the add() method in the given syntax and example:

Syntax

ArrayList<Type> al=new ArrayList<Type>();
al.add("Spring");
al.add("Java");

Example

import java.util.ArrayList;
public class SpringJava{
public static void main(String args[]) {
//creating ArrayList String type and initializing an ArrayList using add() method
ArrayList<String> al=new ArrayList<String>();
al.add("Spring");
al.add("Java");
//printing created ArrayList data
System.out.println("ArrayList: " + al);
  }
}

Output

ArrayList: [Spring, Java]

Initialization with Anonymous Inner class

We can initialize ArrayList in Java using the Anonymous Inner class in the given below example:

Example

import java.util.ArrayList;
public class SpringJava{
public static void main(String args[]) {
//creating ArrayList String type and initializing an ArrayList using anonymous inner class
ArrayList<String> al=new ArrayList<String>(){
{
add("Spring");
add("Java");
  }
};
//printing created ArrayList data
System.out.println("ArrayList: " + al);
    }
}

Output

ArrayList: [Spring, Java]

Initialization using asList() method

We can initialize ArrayList in Java using the asList() method in the given below syntax and example:

Syntax

ArrayList<Type> al = new ArrayList<Type>(Arrays.asList(Obj o1, Obj o2,.....so on));

Example

import java.util.ArrayList;
import java.util.Arrays;
public class SpringJava{
public static void main(String args[]) {
//creating ArrayList String type and initializing an ArrayList using asList() method
ArrayList<String> al=new ArrayList<String>(Arrays.asList("Spring", "Java"));
//printing the created ArrayList data
System.out.println("ArrayList: " + al);
    }
}

Output

ArrayList: [Spring, Java]

Initialization using List.of() method Java 9 feature

We can initialize ArrayList in Java using the List.of() method in the given below syntax and example:

Syntax

ArrayList<Type> al = new ArrayList<Type>(List.of(Obj o1, Obj o2,.....so on));

Example

import java.util.ArrayList;
import java.util.List;
public class SpringJava{
public static void main(String args[]) {
//Creating ArrayList String type and initializing an ArrayList using asList() method
ArrayList<String> al=new ArrayList<String>(List.of("Spring", "Java"));
//printing the created ArrayList data
System.out.println("ArrayList: " + al);
    }
}

Output

ArrayList: [Spring, Java]

Initialization using another Collection

We can initialize ArrayList in Java using another Collection in the given syntax and example:

Syntax

ArrayList<Type> al = new ArrayList<Type>(Collection c);

Example

import java.util.ArrayList;
public class SpringJava{
public static void main(String args[]) {
//creating and initializing another Collection Integer type
ArrayList<Integer> al=new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
//creating and initializing ArrayList Integer type 
ArrayList<Integer> al1=new ArrayList<Integer>(al);
//printing the created ArrayList data
System.out.println("ArrayList: " + al1);
    }
}

Output

ArrayList: [10, 20, 30]

Initialization using Stream API methods Java 8  feature

We can initialize ArrayList in Java using Stream API methods in the given syntax and example:

Syntax

ArrayList<Type> al = Stream.of(element1,element2,element3,..., elementN).collect(Collectors.toCollection(ArrayList::new));

Example

import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SpringJava{
public static void main(String args[])
{
//creating ArrayList String type and initializing with Stream methods
 ArrayList<String> al = Stream.of("Spring", "Java")
     .collect(Collectors.toCollection(ArrayList::new));
 //printing created ArrayList data                 
 System.out.println("ArrayList:"+al); 
    }
}

Output

ArrayList:[Spring, Java]

FAQ

Q1: How to initialize an array of ArrayList in Java?

Ans. We can use Arrays.asList() method to ArrayList from Arrays class in a single line.

Q2: How to initialize the size of an ArrayList in Java?

Ans. We can use the ArrayList constructor by passing size as a parameter to initialize the size of ArrayList like this ArrayList(int initialcapacity).

Q3: How to initialize ArrayList in a single line?

Ans. We can use Arrays.asList(), List.of() and Stream API methods to initialize ArrayList in Java.

Conclusion  

In this topic, we learnt about what is ArrayList class and its various Constructor and how to initialize ArrayList in different ways.

Leave a Comment