目次
1. Introduction
When programming in Java, “List initialization” is one of the most fundamental and important concepts. Unlike arrays, Lists allow dynamic resizing and support various implementations such as ArrayList and LinkedList, making them frequently used in everyday development tasks. However, many developers struggle with questions like “Which initialization method should I use?” or “What are the differences between each approach?” This article clearly explains the essential characteristics of Lists in Java, the purpose of initialization, and the different initialization methods available—especially for beginners. We also cover practical examples commonly used in real projects, common pitfalls, and how to choose the right method depending on your use case. If you want to learn the optimal way to initialize Lists or gain an advantage over competing articles, this guide will be extremely helpful.2. Basics of Lists and the Importance of Initialization
Lists in Java are a type of collection that can store ordered, variable-length data. The most commonly used implementation is ArrayList, but there are several others, including LinkedList and Vector. Compared to arrays, Lists offer flexible resizing and simple operations such as adding or removing elements. 【Features of Lists】- Order is preserved: Elements maintain the order in which they were inserted.
- Duplicates are allowed: Multiple identical values can be stored.
- Dynamic size: No need to specify the size beforehand; elements can be added or removed freely.
3. Five Ways to Initialize a List
Java provides several methods for initializing Lists. The ideal method depends on your use case, Java version, and whether you need to add or remove elements later. This section explains five commonly used initialization techniques with their characteristics and best use cases.3.1 Creating an Empty List
This is the most fundamental initialization approach. It is used when you want to start with an empty List and add values later.List<String> list = new ArrayList<>();- Use case: When adding elements later.
- Key point: Initially empty, but elements can be freely added using
add(). You may also choose other implementations, such as LinkedList, depending on your needs.
3.2 Using Arrays.asList
When you want to quickly create a List from several values or an array,Arrays.asList() is convenient. It allows you to create a List with initial values in a single line.List<String> list = Arrays.asList("A", "B", "C");- Use case: When creating a List from multiple fixed values.
- Note: Lists created with this method have a fixed size, so adding or removing elements using
add()orremove()is not allowed. However,set()can modify existing values. - If modification is required: Create a new ArrayList as follows:
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C"));3.3 Using List.of (Java 9+)
From Java 9 onward,List.of() allows easy creation of immutable Lists.List<String> list = List.of("A", "B", "C");- Use case: When the content is fixed and does not need to change.
- Characteristics: Lists created using this method are fully immutable. No modifications—including
add(),remove(), or evenset()—are allowed. This is ideal for constants and safety-critical data.
3.4 Using an Instance Initializer
This less common technique uses an instance initializer within an anonymous class. It allows multi-line or complex initialization in one expression.List<String> list = new ArrayList<>() {{
add("A");
add("B");
add("C");
}};- Use case: When many elements or complex initialization logic is needed.
- Caution: Because it creates an anonymous class, it is not recommended for large projects or performance-critical environments due to memory and maintainability concerns.
3.5 Creating an ArrayList with an Initial Capacity
List<String> list = new ArrayList<>(100);- Use case: When you expect to insert many elements and already know the approximate size.
- Benefit: Reduces internal resizing operations and improves performance.
4. Comparing Each Initialization Method
This section compares the five initialization techniques introduced earlier. This organized overview helps you decide which method to use when unsure. 【Main Comparison Points】| Initialization Method | Add/Remove | Modify Values | Immutability | Recommended Use Case |
|---|---|---|---|---|
| new ArrayList<>() | Yes | Yes | No | General List operations |
| Arrays.asList(…) | No | Yes | Partial (fixed size) | When converting an array to a List and only modifying existing values |
| new ArrayList<>(Arrays.asList(…)) | Yes | Yes | No | When you need both initial values and modifiable size |
| List.of(…) | No | No | Excellent | When a fully immutable constant List is required |
| Instance initializer | Yes | Yes | No | When initializing complex or multi-line values at once |
| new ArrayList<>(initial capacity) | Yes | Yes | No | When handling many elements and optimizing performance |
- If you need to add or remove elements later ⇒
new ArrayList<>()ornew ArrayList<>(Arrays.asList(...)) - If you want a List from fixed values without adding/removing ⇒
Arrays.asList(...) - If you need a completely immutable List (safety-critical) ⇒
List.of(...)(Java 9+) - If you need multi-line or complex initialization logic ⇒ Instance initializer
- If you expect a large number of elements and want better performance ⇒
new ArrayList<>(initial capacity)
- Lists created using
Arrays.asListhave fixed size—adding or removing elements results in anUnsupportedOperationException. List.ofsupports zero or multiple elements, but it is immutable—add, remove, and set cannot be used.- Instance initializers are powerful but create anonymous classes, which can hurt readability and performance.
5. Practical Usage Examples
This section provides practical examples for each List initialization method introduced earlier. By reviewing concrete scenarios, you can better understand which method suits your own use case.5.1 Creating an Empty List and Adding Values Later
List<String> names = new ArrayList<>();
names.add("Satou");
names.add("Suzuki");
names.add("Takahashi");
System.out.println(names); // Output: [Satou, Suzuki, Takahashi]Explanation: This is the most basic usage. It is useful when you want to prepare an empty List in advance and add values later, such as from user input or loops.5.2 Creating a Fixed-Size List with Arrays.asList
List<String> fruits = Arrays.asList("Apple", "Banana", "Mikan");
System.out.println(fruits); // Output: [Apple, Banana, Mikan]
// fruits.add("Grape"); // ← This will cause an error
fruits.set(0, "Pineapple"); // This is allowed
System.out.println(fruits); // Output: [Pineapple, Banana, Mikan]Explanation: This method is handy for handling a fixed dataset or when you want to process values immediately. However, adding or removing elements is not allowed, so caution is needed.
5.3 Creating an Immutable List with List.of (Java 9+)
List<String> colors = List.of("Red", "Blue", "Green");
System.out.println(colors); // Output: [Red, Blue, Green]
// colors.add("Yellow"); // ← Will throw an exceptionExplanation: This is ideal for constant lists or values that should not be modified, especially when safety and immutability are important.5.4 Setting Complex Initial Values with an Instance Initializer
List<Integer> numbers = new ArrayList<>() {{
for (int i = 1; i <= 5; i++) {
add(i * i); // 1, 4, 9, 16, 25
}
}};
System.out.println(numbers); // Output: [1, 4, 9, 16, 25]Explanation: Useful when you need loops, conditions, or complex logic for initialization. It is powerful but not recommended in large-scale systems due to anonymous class overhead.5.5 Adding Large Amounts of Data with Initial Capacity
List<Integer> bigList = new ArrayList<>(1000);
for (int i = 0; i < 1000; i++) {
bigList.add(i);
}
System.out.println(bigList.size()); // Output: 1000Explanation: When handling large data sets, specifying an initial capacity reduces resizing operations and improves performance. Selecting the correct initialization method based on the real-world scenario improves readability, performance, and maintainability.6. Summary
In this article, we explored multiple approaches to initializing Lists in Java—from basic concepts and practical examples to comparisons and best practices. While List initialization may seem simple at first glance, the optimal method varies significantly depending on use cases and requirements. Key Points Recap:- Lists are ordered, allow duplicates, and support dynamic resizing, making them more flexible than arrays.
- Java provides various initialization methods: empty Lists, Lists with initial values, immutable Lists, Lists with specified initial capacity, and more.
- Choosing the right method depends on whether you need to add/remove elements, handle fixed data, ensure immutability, or manage large datasets efficiently.
Arrays.asListandList.ofhave specific limitations (fixed size or full immutability), so understanding their behavior is essential.
7. Frequently Asked Questions (FAQ)
Q1: Can I add elements to a List created with Arrays.asList? A1: No, you cannot. A List created usingArrays.asList has a fixed size, so calling add() or remove() will throw an UnsupportedOperationException.
However, you can overwrite existing values using set().
If you want a modifiable List, convert it as follows: new ArrayList<>(Arrays.asList(...)) Q2: What is the difference between List.of and Arrays.asList? A2:List.of(Java 9+) creates a fully immutable List. No modifications are allowed—not evenset().Arrays.asListcreates a fixed-size List. You cannot add or remove elements, but overwriting values withset()is allowed.
add() and remove(), but List.of provides stronger immutability.
If safety and immutability are priorities, List.of is recommended. Q3: What are the benefits of specifying an initial capacity when initializing a List? A3:
When using ArrayList, specifying an initial capacity is beneficial when you expect to add many elements. It reduces internal array resizing operations, which improves performance.
If you know the approximate number of elements in advance, setting an initial capacity avoids unnecessary memory reallocations. Q4: Why should I be careful when using an instance initializer for initialization? A4:
An instance initializer creates an anonymous class behind the scenes.
This can lead to:- Increased memory usage
- Reduced maintainability
- Potential issues during serialization
List<String> list = new LinkedList<>(); You can also initialize a LinkedList using other methods:new LinkedList<>(existingList)- Using
Arrays.asListorList.ofcombined with conversion


