It would be simpler if you were to just declare it as a fixed-size List (but with mutable elements) - does it have to be an ArrayList?

List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

Or if you have only one element:

List<String> places2 = Collections.singletonList("Buenos Aires");

This would mean that places2 is immutable (trying to change it in any way will cause an UnsupportedOperationException exception to be thrown).

To make a variable-size list that is a concrete ArrayList you can create an ArrayList from the fixed-size list:

ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));

And import the correct package:

import java.util.Arrays;
Answer from Tom on Stack Overflow
Top answer
1 of 16
2789

It would be simpler if you were to just declare it as a fixed-size List (but with mutable elements) - does it have to be an ArrayList?

List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

Or if you have only one element:

List<String> places2 = Collections.singletonList("Buenos Aires");

This would mean that places2 is immutable (trying to change it in any way will cause an UnsupportedOperationException exception to be thrown).

To make a variable-size list that is a concrete ArrayList you can create an ArrayList from the fixed-size list:

ArrayList<String> places = new ArrayList<>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));

And import the correct package:

import java.util.Arrays;
2 of 16
2240

Actually, probably the "best" way to initialize the ArrayList is the method you wrote, as it does not need to create a new List in any way:

ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");

The catch is that there is quite a bit of typing required to refer to that list instance.

There are alternatives, such as making an anonymous inner class with an instance initializer (also known as an "double brace initialization"):

ArrayList<String> list = new ArrayList<String>() {{
    add("A");
    add("B");
    add("C");
}};

However, I'm not too fond of that method because what you end up with is a subclass of ArrayList which has an instance initializer, and that class is created just to create one object -- that just seems like a little bit overkill to me.

What would have been nice was if the Collection Literals proposal for Project Coin was accepted (it was slated to be introduced in Java 7, but it's not likely to be part of Java 8 either.):

List<String> list = ["A", "B", "C"];

Unfortunately it won't help you here, as it will initialize an immutable List rather than an ArrayList, and furthermore, it's not available yet, if it ever will be.

🌐
GeeksforGeeks
geeksforgeeks.org › java › initialize-an-arraylist-in-java
Initialize an ArrayList in Java - GeeksforGeeks
July 11, 2025 - ArrayList inherits the AbstractList class and implements the List interface. ArrayList is initialized by a size, however, the size can increase if the collection grows or shrink if objects are removed from the collection.
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
It is part of the java.util package and implements the List interface. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).
🌐
Baeldung
baeldung.com › home › java › java list › java list initialization in one line
Java List Initialization in One Line | Baeldung
April 4, 2025 - With the outer braces, we declare an anonymous inner class that will be a subclass of the ArrayList. We can declare the details of our subclass inside these braces. As usual, we can use instance initializer blocks, and that is where the inner pair of braces comes from. The brevity of this syntax is tempting. However, it’s considered an anti-pattern. To read more about double-brace initialization, have a look at our article here. Modern Java offers several options to create a Collection in one line.
🌐
Runestone Academy
runestone.academy › ns › books › published › csawesome › Unit7-ArrayList › topic-7-1-arraylist-basics.html
7.1. Intro to ArrayLists — CSAwesome v1
To declare a ArrayList use ArrayList<Type> name where Type, called a type parameter is the type of objects you want to store in the ArrayList. For example a variable naming an ArrayList meant to hold Strings is declared as ArrayList<String> as shown in the code below.
🌐
Blogger
javarevisited.blogspot.com › 2012 › 12 › how-to-initialize-list-with-array-in-java.html
How to declare and initialize a List with values in Java ...
Couple of ways to create and initialize an ArrayList from values of Array in Java. One of the popular example is creating ArrayList of String and populating them from String array.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-initialize-an-arraylist
How to Initialize an ArrayList in Java
One of the ways to initialize an ArrayList is to create it first and then add elements later using add() method. import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { // Creating an empty ArrayList with String type ArrayList<String> names = new ...
Find elsewhere
🌐
Oracle
forums.oracle.com › ords › apexds › post › how-do-i-instantiate-and-initialize-an-arraylist-in-a-singl-1591
How do I instantiate and initialize an ArrayList in a single line of code? - Oracle Forums
October 16, 2007 - Please pardon my stupidity, but is there a way to do this? The javadocs say you can pass a Collection to an ArrayList constructor, but Collections appear to be constructed in the sa...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-initialize-an-arraylist-in-java-with-values
How to Initialize an ArrayList in Java – Declaration with Values
April 21, 2023 - You'll see the different in-built methods that can be used to add, access, modify, and remove elements in an ArrayList. Let's get started! The terms "declaration" and "initialization" are commonly associated with data structures. Declaration has to do with creating a data structure, while initialization involves assigning values to the data structure. ... import java.util.ArrayList; public class ArrayListTut { public static void main(String[] args) { ArrayList<String> people = new ArrayList<>(); } }
🌐
Quora
quora.com › In-Java-whats-the-best-way-to-create-and-initialize-an-ArrayList-Whats-the-best-way-to-do-it-in-one-line
In Java, what's the best way to create and initialize an ArrayList? What's the best way to do it in one line? - Quora
Answer (1 of 10): The obvious methods have been listed by other members, like [code ]Arrays.asList(oneString, twoString, threeString).[/code] I looked at the mature Java library called “Guava” and saw this method: newArrayList(E... elements); That seems to be the most elegant approach, ...
🌐
CodeJava
codejava.net › java-core › collections › initialize-list-with-values
Java Initialize ArrayList with Values in One Line
July 18, 2024 - public class User { String name; ... construct an ArrayList object that wraps the list returned by Arrays.asList() method. Since Java 9, you can use the List.of() method to initialize a List with some initial values in a single line....
🌐
Career Karma
careerkarma.com › blog › java › how to initialize arraylist in java
How to Initialize ArrayList in Java | Career Karma
December 1, 2023 - This tutorial will explore, with examples, how to initialize an ArrayList in Java using the asList() method.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - This class is a member of the Java Collections Framework. ... Constructs an empty list with the specified initial capacity. ... Constructs an empty list with an initial capacity of ten. ... Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. ... Trims the capacity of this ArrayList instance to be the list's current size.
🌐
Verve AI
vervecopilot.com › interview-questions › can-java-arraylist-initialize-be-the-secret-weapon-for-acing-your-next-interview
Can Java Arraylist Initialize Be The Secret Weapon For Acing Your Next… · For Acing Your Next Interview · Interview Q&A | Verve AI
When you `java arraylist initialize` using the default constructor (`new ArrayList<>()`), it creates an internal array with an initial capacity (typically 10 elements). As you add more elements, if the `ArrayList`'s internal array reaches its ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › initializing-a-list-in-java
Initializing a List in Java - GeeksforGeeks
The most common way to create and initialize a List is by using the ArrayList class.
Published   October 11, 2025
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-in-java
Arrays in Java - GeeksforGeeks
You can use array literals to initialize an array when declaring it. In this case, the new keyword is not required- ... The length of this array determines the length of the created array.
Published   May 8, 2026
🌐
W3Schools
w3schools.com › java › java_arrays_multi.asp
Java Multi-Dimensional Arrays
Java Data Structures Java Collections Java List Java ArrayList Java LinkedList Java List Sorting Java Set Java HashSet Java TreeSet Java LinkedHashSet Java Map Java HashMap Java TreeMap Java LinkedHashMap Java Iterator Java Algorithms
🌐
W3Schools
w3schools.com › java › java_constructors.asp
Java Constructors
A constructor in Java is a special method that is used to initialize objects.
🌐
NeetCode
neetcode.io › problems › group anagrams
Group Anagrams - NeetCode
public class Solution { public List<List<String>> groupAnagrams(String[] strs) { Map<String, List<String>> res = new HashMap<>(); for (String s : strs) { char[] charArray = s.toCharArray(); Arrays.sort(charArray); String sortedS = new String(charArray); res.putIfAbsent(sortedS, new ArrayList<>()); res.get(sortedS).add(s); } return new ArrayList<>(res.values()); } }