Java Programming
Data Structures
Coding Tutorial
String Initialization
List Objects

How to initialize List<String> object in Java?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In Java, a List<String> is a collection that holds an ordered sequence of elements, each of which is a String. This data structure is part of Java's Collection Framework, available since Java 1.2, and it provides a more flexible replacement for the traditional arrays.

Ways to Initialize a List of Strings

When it comes to initializing a List<String> in Java, there are several approaches, each suitable for different scenarios. Here are some of the most common methods:

Using the Arrays.asList() Method

If you have a fixed set of strings and you will not need to modify the list (add or remove elements) after its creation, Arrays.asList() is a convenient method:

java
1import java.util.List;
2import java.util.Arrays;
3
4List<String> list = Arrays.asList("Apple", "Banana", "Cherry");

This creates a fixed-size list backed by an array, so while you can replace elements, trying to add or remove elements will throw an UnsupportedOperationException.

Using Java Collections Framework

For a flexible list where items can be added or removed dynamically, you can use classes that implement the List interface, such as ArrayList or LinkedList:

java
1import java.util.ArrayList;
2import java.util.List;
3
4List<String> list = new ArrayList<>();
5list.add("Dog");
6list.add("Cat");
7list.add("Horse");

ArrayList is generally the preferred option due to better performance in locating elements and faster iteration. LinkedList might be preferred when frequent addition and removal of elements are the senior operations due to less shifting of elements.

Java 9 Enhancements

Starting from Java 9, you can use the List.of() static factory method to create an immutable list:

java
List<String> list = List.of("X", "Y", "Z");

Like Arrays.asList(), the list created using List.of() is immutable, but it's completely unmodifiable, which means adding or removing elements will throw an UnsupportedOperationException.

Initialization via Double Brace &#123;&#123; &#125;&#125;

Although it's not generally recommended due to potential memory leaks and more obscure code behavior, a double brace initialization can still be found in various Java codes:

java
1List<String> list = new ArrayList<String>() {{
2    add("Element1");
3    add("Element2");
4    add("Element3");
5}};

This method uses an anonymous inner class that inherits from ArrayList. The instance initializer block &#123;&#125; is used to add elements. This can lead to subtle bugs and memory issues because the class has a hidden reference to the enclosing instance.

Summary Table

MethodMutableFixed SizeRecommended
Arrays.asList()NoYesFor fixed-size initialization
ArrayList, LinkedListYesNoYes, for mutable lists
List.of()NoYesFor immutable lists
Double Brace InitializationYesNoNo, due to potential issues

Additional Considerations

Performance

The choice between ArrayList and LinkedList should be based on the specific needs:

  • ArrayList: Better for reading data frequently due to faster random access.
  • LinkedList: More efficient for operations involving frequent addition and removal of elements.

Immutability

Immutable lists are safer from accidental modifications, which can be critical in concurrent applications or shared data structures. If immutable structures are a requirement, consider using Collections.unmodifiableList() on your existing list.

Conversions

Sometimes, you might need to convert a list to an array or vice versa. Use the toArray() method to convert a list to an array, and the methods mentioned above to convert an array to a list.

In conclusion, initializing and manipulating List<String> in Java can be done in several ways, each suitable for different circumstances considering factors like mutability, performance, and the need for fixed or dynamic sizing. Choose the method that best fits the requirements of your specific scenario.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.