Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Initialize an ArrayList in Java

Java ArrayList can be initialized in one line using List.of(), and new ArrayList constructors. Learn to add elements one by one and in bulk.

Lokesh Gupta

August 4, 2023

Java ArrayList
Java ArrayList
ArrayList

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
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

ArrayList Methods

  • ArrayList add()
  • ArrayList addAll()
  • ArrayList clone()
  • ArrayList contains()
  • ArrayList ensureCapacity()
  • ArrayList forEach()
  • ArrayList get()
  • Arraylist indexOf()
  • Arraylist lastIndexOf()
  • ArrayList listIterator()
  • ArrayList remove()
  • ArrayList removeAll()
  • ArrayList removeIf()
  • ArrayList retainAll()
  • ArrayList spliterator()
  • ArrayList subList()
  • ArrayList toArray()

ArrayList Examples

  • Initialize Arraylist
  • Iteration
  • Add/replace Element
  • Add Multiple Elements
  • Check Empty List
  • Remove Element
  • Replace Element
  • Empty ArrayList
  • Synchronized ArrayList
  • Compare two lists
  • Remove duplicates
  • Merge two lists
  • Serialize a List
  • Swap two elements
  • Convert Array to ArrayList
  • Convert HashSet to ArrayList
  • Convert LinkedList to ArrayList
  • ArrayList vs LinkedList
  • ArrayList vs Vector

Table of Contents

  • 1. Initialize ArrayList in One Line
    • 1.1. Use Arrays.asList() to Initialize ArrayList from Array
    • 1.2. Using List.of() [Java 9 and above]
  • 2. Initialize ArrayList with Constructors
  • 3. Initialize ArrayList from Java 8 Stream
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java String intern()

Next

Different Ways to Iterate an ArrayList

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.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap