Java

How to Create an ArrayList in Java

ArrayList is the most fundamental and widely used data structure in Java. It is a component of the Collection Framework in Java. It behaves like a typical array and offers all of its advantages, but it also implements the List interface as a generic re-sizable collection. The Java ArrayList is particularly useful for managing multiple objects.

This blog will define the methods for creating Java ArrayList.

How to Create an ArrayList in Java?

The ArrayList in Java implements the List interface, it extends the Collection interface, and also it is a subclass of AbstractList. As a result, we have the following options for declaring an ArrayList object:

Let’s go through the working of these approaches and how they help in creating an ArrayList in Java.

Method 1: Create an ArrayList in Java Using Collection Interface

In this section, we will create an ArrayList using a “Collection” interface that belongs to the java.util.Collections package. It converts all the elements of one collection to another one by one.

Syntax

Use the below syntax for creating an ArrayList using Collection Interface:

Collection<String> name = new ArrayList<>();

Here, “name” represents our ArrayList that is created using Collection Interface.

Example

Using Collection Interface, we will create an ArrayList called “fruits“:

Collection<String> fruits = new ArrayList<>();

Then, add the elements in the ArrayList using the “add()” method:

fruits.add("Orange");

fruits.add("Mango");

fruits.add("Grapes");

fruits.add("Peach");

fruits.add("Apricot");

Lastly, print the ArrayList on the console with the “System.out.println()” method:

System.out.println(fruits);

The output shows that the ArrayList is successfully created using Collection Interface:

Let’s try the List interface for the creation of ArrayList in Java.

Method 2: Create an ArrayList in Java Using List Interface

We will use the List interface for creating an ArrayList. It implements classes such as LinkedList, ArrayList, Stack, and so on. The List interface belongs to java.util, and it is a child interface of Collection. Like an Array, it is also index-based and permits you to store duplicate values.

Syntax

Follow the below-given syntax for using List to create an ArrayList:

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

Here, “name” represents our ArrayList that is created using List Interface.

Example

Create an empty ArrayList named “colors” using the List interface:

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

Call the “add()” method to add the elements in the ArrayList:

colors.add("Red");

colors.add("Black");

colors.add("Blue");

colors.add("White");

colors.add("Green");

colors.add("Purple");

Finally, we will print the ArrayList elements on the console:

System.out.println(colors);

Output

If you want to create an ArrayList using the ArrayList class, then follow the below section:

Method 3: Create an ArrayList in Java Using ArrayList Class

In this section, we will create an ArrayList using the ArrayList class. The ArrayList class belongs to java.util package. This approach stores an element in a dynamic array with no size restriction. You can utilize all of the List interface’s methods because it implements the List interface. Moreover, the ArrayList keeps track of the insertion order implicitly.

Syntax

The following provided syntax will be used for the creation of an ArrayList using the ArrayList class:

ArrayList<String> name = new ArrayList<>();

Here, “name” represents our ArrayList that is created using the ArrayList class.

Example

We will create a “gadgets” ArrayList using the ArrayList class:

ArrayList<String> gadgets = new ArrayList<>();

Then, add elements in the ArrayList using the “add()” method:

gadgets.add("Mobile");

gadgets.add("Laptop");

gadgets.add("Computer");

gadgets.add("Stylus");

Lastly, we will print the elements of ArrayList passing the ArrayList to the “System.out.println()” method:

System.out.println(gadgets);

Output

We have provided different approaches for creating an ArrayList in Java.

Conclusion

For creating an ArrayList in Java, you can use the Collection interface, List interface, and ArrayList class. All these approaches successfully created an ArrayList. The most commonly used approach by programmers for creating an ArrayList is utilizing the ArrayList class. In this post, we have provided different approaches for creating an ArrayList in Java with detailed examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.