Question

How to Create a List in Java

javalistcollections

Question

Given that a Set can be created with HashSet, how do you create a List in Java?

For example:

Set mySet = new HashSet();

What is the equivalent way to declare and instantiate a List?

Short Answer

You will learn that List is a Java interface and is usually created using a concrete implementation such as ArrayList or LinkedList. You will also learn why generic types such as List<String> are important and how to choose an appropriate list implementation.

Concept

A List is an ordered collection in Java. Unlike a Set, a list:

  • Keeps elements in insertion order.
  • Allows duplicate values.
  • Lets you access elements by their numeric position, called an index.

For example, a list can store the values "red", "blue", and "red" again. The first item has index 0, the second has index 1, and so on.

List belongs to the Java Collections Framework and is an interface. An interface describes available operations, but it cannot create objects itself. Therefore, you declare a variable using List and instantiate a class that implements it.

The most common implementation is ArrayList:

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

This approach is preferred because your code depends on the general List contract rather than a specific implementation. You can later change ArrayList to LinkedList with minimal changes if your needs change.

Mental Model

Think of a List as a numbered shelf.

  • Each shelf position has a number: 0, 1, 2, and so on.
  • You can put the same item on more than one shelf position.
  • Items stay in the order in which you place them.

List is the label describing what the shelf system can do: add, remove, find, and retrieve items by position. ArrayList is one particular kind of shelf system that provides those operations.

In the same way, List<String> describes what you need, while new ArrayList<>() specifies how it is built.

Take Quiz

Syntax and Examples

Import the List interface and an implementation such as ArrayList:

import java.util.ArrayList;
import java.util.List;

Create a list with a generic element type:

List<String> fruits = new ArrayList<>();

fruits.add("Apple");
fruits.add("Banana");
fruits.add("Apple");

System.out.println(fruits); // [Apple, Banana, Apple]
System.out.println(fruits.get(0)); // Apple

List<String> means this list may contain only String values. The diamond operator, <>, lets Java infer the same type on the right side.

You can also create a list with initial values:

List<Integer> scores = new ArrayList<>(List.of(10, 20, 30));

scores.add(40);
System.out.println(scores); // [10, 20, 30, 40]

new ArrayList<>(List.of(...)) creates a mutable ArrayList containing the supplied values.

Step by Step Execution

Consider this program:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> tasks = new ArrayList<>();

        tasks.add("Email customer");
        tasks.add("Write report");
        tasks.add("Deploy application");

        String firstTask = tasks.get(0);
        tasks.remove("Write report");

        System.out.println(firstTask);
        System.out.println(tasks);
    }
}

Execution steps:

  1. List<String> tasks declares a variable that can refer to a list of strings.
  2. new ArrayList<>() creates an empty, mutable list.
  3. The first add places "Email customer" at index 0.
  4. The second and third add calls place items at indices 1 and 2.
  5. get(0) reads the item at index 0, so firstTask becomes "Email customer".
  6. remove("Write report") removes that matching value. The remaining items shift left to close the gap.
  7. The output is:
Email customer
[Email customer, Deploy application]

Real World Use Cases

Lists are useful whenever order matters or duplicate values are valid.

  • Shopping carts: Store products in the order a customer adds them.
  • API responses: Return an ordered collection of search results, messages, or invoices.
  • Form validation: Keep a list of validation error messages to show a user.
  • Task queues: Maintain a sequence of jobs that must be processed.
  • CSV and file processing: Read lines or records into a collection while preserving file order.
  • User interface data: Store rows for a table, notifications, menu items, or chat messages.

For example, an API service might collect validation errors:

List<String> errors = new ArrayList<>();

if (email == null || email.isBlank()) {
    errors.add("Email is required.");
}

if (password == null || password.length() < 8) {
    errors.add("Password must contain at least 8 characters.");
}
Take Quiz

Real Codebase Usage

In production code, developers usually program to the List interface and choose an implementation based on normal usage.

private final List<String> auditEvents = new ArrayList<>();

Common patterns include:

  • Use List<T> in variable declarations, parameters, and return types. This keeps code flexible.
public List<String> findUsernames() {
    return new ArrayList<>();
}
  • Use ArrayList by default. It is usually a good choice for appending items and reading by index.

  • Validate before adding data. Guard clauses prevent invalid values from entering a list.

public void addTag(List<String> tags, String tag) {
    if (tag == null || tag.isBlank()) {
        return;
    }

    tags.add(tag.trim());
}
  • Return read-only views when callers should not modify a result.
public List<String> supportedFormats() {
    return List.of("json", "xml", "csv");
}

List.of(...) returns an unmodifiable list. It is useful for fixed configuration values, but calling add, remove, or set on it throws UnsupportedOperationException.

  • Use collection operations for transformations. For example, streams can filter list data, though straightforward loops remain clear for many tasks.

Common Mistakes

Using raw types

This compiles, but it removes type safety:

List items = new ArrayList();
items.add("text");
items.add(42);

Avoid raw types. Specify the element type:

List<String> items = new ArrayList<>();
items.add("text");

Trying to instantiate List directly

This does not work because List is an interface:

List<String> names = new List<>(); // Does not compile

Create an implementing class instead:

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

Forgetting that indices start at zero

List<String> names = List.of("Ana", "Bo");
System.out.println(names.get(2)); // Error: valid indices are 0 and 1

For a list with size() elements, the valid indices are from 0 through size() - 1.

Modifying an unmodifiable list

List<String> names = List.of("Ana", "Bo");
names.add("Cy"); // Throws UnsupportedOperationException

Use a mutable copy when modifications are needed:

List<String> names = new ArrayList<>(List.of("Ana", "Bo"));
names.add("Cy");

Removing an integer value by accident

For List<Integer>, remove(1) means “remove the item at index 1,” not “remove the value 1.”

List<Integer> numbers = new ArrayList<>(List.of(1, 2, 3));
numbers.remove(1); // Removes 2, the value at index 1

To remove the value 1, use Integer.valueOf(1):

numbers.remove(Integer.valueOf(1));

Comparisons

Collection or implementationOrderingDuplicatesTypical use
ListPreserves insertion orderAllowedOrdered sequences, items accessed by index
SetDepends on implementationNot allowedUnique values, such as tags or IDs
ArrayListPreserves insertion orderAllowedDefault list choice; fast indexed reads and appending
LinkedListPreserves insertion orderAllowedOccasional use when repeatedly adding/removing at list ends

ArrayList vs LinkedList

FeatureArrayListLinkedList
Read an item by indexUsually fastUsually slower because it traverses nodes
Add at the endUsually fastFast
Memory overheadLowerHigher per item
Good default choiceYesUsually no, unless its linked-node behavior is specifically useful

For most beginner and application-level code, start with:

List<String> values = new ArrayList<>();
Take Quiz

Cheat Sheet

import java.util.ArrayList;
import java.util.List;

// Empty mutable list
List<String> names = new ArrayList<>();

// Mutable list with initial items
List<String> names = new ArrayList<>(List.of("Ana", "Bo"));

// Fixed, unmodifiable list
List<String> names = List.of("Ana", "Bo");
TaskCode
Add an itemnames.add("Cy");
Read by indexnames.get(0);
Replace by indexnames.set(0, "Ada");
Remove by indexnames.remove(0);
Remove by valuenames.remove("Ada");
Count itemsnames.size();
Check for a valuenames.contains("Ada");
Loop through itemsfor (String name : names) { ... }

Key rules:

  • Prefer List<Type> variable = new ArrayList<>();.
  • List is an interface; ArrayList is a common implementation.
  • List indexes begin at 0.
  • Lists allow duplicates and preserve order.
  • List.of(...) is unmodifiable; wrap it in new ArrayList<>(...) to modify it.

FAQ

How do I create an empty List in Java?

Use ArrayList:

List<String> items = new ArrayList<>();

Import java.util.List and java.util.ArrayList.

Why use List<String> instead of just List?

List<String> restricts the list to strings and lets the compiler catch incorrect additions. A raw List can hold mixed types and requires unsafe casts.

Can a Java List contain duplicate elements?

Yes. A list preserves order and allows duplicates. This is a key difference from a Set.

Can I write new List<>() in Java?

No. List is an interface, not a class. Use an implementation such as new ArrayList<>().

Which List implementation should I use in Java?

Use ArrayList by default. Consider another implementation only when you have a specific need and understand its trade-offs.

Is List.of() mutable?

No. List.of() creates an unmodifiable list. To add or remove items later, create a mutable copy with new ArrayList<>(List.of(...)).

How do I get the first element of a List?

Use index 0, after ensuring the list is not empty:

if (!items.isEmpty()) {
    String first = items.get(0);
}
Take Quiz

Mini Project

Description

Build a small shopping-list manager for a command-line application. The program demonstrates creating a mutable List, adding entries, reading items by index, removing an entry, and safely handling an empty list.

Goal

Create and update an ordered shopping list using List<String> and ArrayList.

Requirements

Use List<String> with an ArrayList implementation.|Add at least three shopping items in a defined order.|Display each item with its zero-based index.|Remove one item by its value.|Print the first remaining item only when the list is not empty.

Take Quiz

Keep learning

Related questions

View more