How to initialize ArrayList in Java

The Java ArrayList can be initialized in number of ways depending on the requirement. In this tutorial, we will learn to initialize ArrayList based on some frequently seen usecases.

Table of Contents

1. Initialize ArrayList in single line
2. Create ArrayList and add objects
3. Initialize arraylist of lists

1. Initialize ArrayList in one line

1.1. Arrays.asList() – Initialize arraylist from array

To initialize an arraylist in single line statement, get all elements in form of array using Arrays.asList method and pass the array argument to ArrayList constructor.

ArrayList<String> names = new ArrayList<String>( Arrays.asList("alex", "brian", "charles") );

System.out.println(names);

Program output.

[alex, brian, charles]

1.2. List.of() – Immutable list – Java 9

We can use List.of() static factory methods to create immutable lists. Only drawback is that add operation is not supported in these lists.

List<String> names = List.of("alex", "brian");

System.out.println(names);

Program output.

[alex, brian]

Read More : Java 9 Immutable Collections

2. Create ArrayList and add objects – ArrayList constructor

Using ArrayList constructor is traditional approach. We create a blank arraylist using constructor and add elements to list using add() method. We can add elements either one by one, or we can pass another collection to add all elements in one step.

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

//1. Add elements one by one
names.add("alex");
names.add("brian");
names.add("charles");

System.out.println(names);

HashMap<String, Integer> details = new HashMap<>();

details.put("keanu", 23);
details.put("max", 24);
details.put("john", 53);

//2. Add elements from other collection
names.addAll(details.keySet());

System.out.println(names);

Program output.

[alex, brian, charles]
[alex, brian, charles, max, john, keanu]

3. Initialize arraylist of lists

At times, we may need to initialize arraylist of lists.

List<List<Integer>> marks = new ArrayList<>();
        
marks.add( Arrays.asList(10, 20, 30) );
marks.add( Arrays.asList(40, 50, 60) );
marks.add( Arrays.asList(70, 80, 90) );

for (List<Integer> mark : marks) {
   System.out.println(mark);
}

Program output.

[10, 20, 30]
[40, 50, 60]
[70, 80, 90]

Please note that Arrays.asList() does not return java.util.ArrayList instance. It returns java.util.Arrays$ArrayList instance instead.

So if you must have an ArrayList only, then create ArrayList for Arrays.asList() instance in below manner.

marks.add(new ArrayList<Integer>( Arrays.asList(10, 20, 30) ));

That’s all about to create an arraylist in Java. Drop me your questions in comments.

Happy Learning !!

Reference:

ArrayList Java Docs
A Guide to Java ArrayList

Was this post helpful?

Join 8000+ Awesome Developers, Like YOU!

1 thought on “How to initialize ArrayList in Java”

Leave a Comment

About HowToDoInJava

This blog provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.