# Java Create a New List with ArrayList and List.of

> Create a new list in Java using ArrayList for mutable lists or List.of for immutable lists, with examples showing element access, modification, and common exceptions

**URL:** https://sentry.io/answers/how-do-i-create-a-new-list-in-java/

---

## The Problem

You don't know how to create a new list in Java

## The Solution

The most common way to create a new list in Java is to use the `ArrayList` class from the `java.util` package, as it offers more flexibility when managing and adding elements than other list types, while still implementing the `List` interface for abstraction.

However, if you require your list to be immutable, you can use `List` directly. This is useful if you need your list to be read-only.

### Using `ArrayList`

The `ArrayList` class implements the `List` interface. Here's an example:

```java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Main {
    public static void main(String[] args) {
        List<String> snackList = new ArrayList<>(Arrays.asList("Apple", "Baguette", "Chai"));
        System.out.println("My List: " + snackList); // Prints My List: [Apple, Baguette, Chai]
    }
}
```

You can then use methods to manipulate list elements. For example, you can use the [`add()`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ArrayList.html#add(E)) method to insert another element into the above list:

```java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Main {
    public static void main(String[] args) {
        List<String> snackList = new ArrayList<>(Arrays.asList("Apple", "Baguette", "Chai"));
        snackList.add("Donut");
        System.out.println("My List: " + snackList); // Prints My List: [Apple, Baguette, Chai, Donut]
    }
}
```

### Using `List.of()`

In Java version 9 and later, you can create an immutable list using the `List.of()` method:

```java
import java.util.List;

class Main {
    public static void main(String[] args) {
        List<String> snackList = List.of("Apple", "Baguette", "Chai");

        System.out.println("My List: " + snackList); // Prints My List: [Apple, Baguette, Chai]
    }
}
```

This creates an immutable list, meaning you cannot add or remove elements after creating it. Attempting to do so will throw an `UnsupportedOperationException`:

```java
import java.util.List;

class Main {
    public static void main(String[] args) {
        List<String> snackList = List.of("Apple", "Baguette", "Chai");

        // This will throw an error.
        snackList.add("Donut");
    }
}
```

```java
Exception in thread "main" java.lang.UnsupportedOperationException
    at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:71)
    at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.add(ImmutableCollections.java:75)
    at Main.main(Main.java:7)
```

## Choosing Between Different List Creation Methods

When deciding how to create a list, consider the following:

- The `ArrayList` method is best used when you require flexibility to modify elements.
- The `List.of()` method is best used when you need a list for constants.

---

*Source: [sentry.io/answers/how-do-i-create-a-new-list-in-java/](https://sentry.io/answers/how-do-i-create-a-new-list-in-java/)*
