Initialize an ArrayList in Java

The Java ArrayList represents a resizable array of objects which allows us to add, remove, find, sort and replace elements. The ArrayList is part of the Collection framework and implements in the List interface.

We can initialize an ArrayList in a number of ways depending on the requirement. In this tutorial, we will learn to initialize ArrayList based on some frequently seen usecases.

 // 1 - Empty ArrayList with initial capacity 10
ArrayList<String> list = new ArrayList<>();  

//2 - Empty ArrayList with initial capacity 64
ArrayList<String> list = new ArrayList<>( 64 );   

//3 - Initialize and populate arraylist in one line
ArrayList<String> names = new ArrayList<>( Arrays.asList(...) );  
ArrayList<String> names = new ArrayList<>( List.of(...) );

//4 - Initialize arraylist from existing collection
 ArrayList<String> list = new ArrayList<>( collection );  

//5 - Collect stream elements
List<String> list = stream.collect(Collectors.toCollection(ArrayList::new));

1. Initialize ArrayList in One Line

Creating and initializing the ArrayList in different statements sometimes seems to generate unnecessary boilerplate code. We can optimize the ArrayList creation using the following ways.

1.1. Use Arrays.asList() to Initialize ArrayList from Array

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

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

1.2. Using List.of() [Java 9 and above]

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

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

2. Initialize ArrayList with Constructors

Using the ArrayList constructor is the traditional approach. We initialize an empty ArrayList (with the default initial capacity of 10) using the no-argument constructor and add elements to the list using add() method.

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

names.add("alex");   //Adding a single element at a time
names.add("brian");
names.add("charles");

Optionally, the ArrayList constructor takes an optional parameter initialCapacity. It must be a positive integer and denotes the initial capacity of the list. The initial capacity represents the number of elements that the ArrayList can hold without resizing its internal array.

The following example creates an arraylist of integers with an initial capacity of 64. It means that list resizing will happen when we add the 65th element. For the first 64 elements, by avoiding the resizing operation, we improve the performance.

ArrayList<Integer> list = new ArrayList<>(64);

We can add elements one by one or pass another collection to addAll() elements in one step. It is helpful in initializing an arraylist with values or existing objects from another collection of any type.

HashMap<String, Integer> details = new HashMap<>();
 
details.put("keanu", 23);
details.put("max", 24);
details.put("john", 53);
 
names.addAll(details.keySet());   //Adding multiple elements in ArrayList

3. Initialize ArrayList from Java 8 Stream

A Stream represents a sequence of elements that allows to perform operations on collections of objects in a functional programming style. We can collect the objects from the stream into a new ArrayList as well.

Stream<String> stream = Stream.of("alex", "brian", "charles");

ArrayList<String> stringList = stream
  //perform stream operations
  .collect(Collectors.toCollection(ArrayList::new));

That’s all about initializing an ArrayList in Java. Drop me your questions in the comments.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

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

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

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode