# Java Convert an Array to a List Four Different Ways

> Convert an array to a List using Arrays.asList(), the ArrayList constructor for a mutable list, Collections.addAll(), or the Java 8+ Stream API

**URL:** https://sentry.io/answers/how-to-convert-an-array-to-a-list-in-java/

---

## The Problem

I don't know how to convert an array to a `List` in Java.

## The Solution

In Java, you can convert an array to a `List` in several ways, depending on your requirements and the Java version you are using.

We'll demonstrate how to use the following:

- The `Arrays.asList()` method
- The `ArrayList` constructor
- The `Collections.addAll()` method
- The Java 8+ `Stream` API

### Using `Arrays.asList()`

The `Arrays.asList()` method is the simplest way to convert an array to a `List`.

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

public class Main {
    public static void main(String[] args) {
        String[] array = {"Apple", "Banana", "Cherry"};
        List<String> list = Arrays.asList(array);
        System.out.println("List: " + list);
    }
}
```

Lists returned by `Arrays.asList()` are fixed-size, meaning list elements cannot be added or removed. Attempts to add or remove list elements throw an `UnsupportedOperationException`.

### Using `ArrayList`

The result of the `Arrays.asList()` method can then be passed to the `ArrayList` constructor to create a mutable list.

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

public class Main {
    public static void main(String[] args) {
        String[] array = {"Apple", "Banana", "Cherry"};

        List<String> list = new ArrayList<>(Arrays.asList(array));

        list.add("Berry"); // This adds a new element to the list
        System.out.println("List: " + list);
    }
}
```

You can add and remove elements to mutable lists like the one created by the `ArrayList` constructor above.

### Using `Collections.addAll()`

The `Collections.addAll()` method can be used to convert an array to a `List` by adding the array elements to an existing list.

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

public class Main {
    public static void main(String[] args) {
        String[] array = {"Cherry"};
        List<String> fruitsList = new ArrayList<>(Arrays.asList("Apple", "Banana"));

        Collections.addAll(fruitsList, array); // This adds the array with a "Cherry" element to the list of fruits
        System.out.println("List: " + fruitsList);
    }
}
```

This outputs the following:

```
List: [Apple, Banana, Cherry]
```

### Using Java Streams

If you are using Java 8 or later, you can use the `Stream` API to convert an array to a `List`.

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        String[] array = {"Apple", "Banana", "Cherry"};
        List<String> list = Arrays.stream(array).collect(Collectors.toList());
        System.out.println("List: " + list);
    }
}
```

This method is useful if you need to perform additional stream operations, such as filtering or mapping, during the conversion.

For example, you can convert an array to a list and filter out elements that start with a specific letter:

```java
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        String[] array = {"Apple", "Banana", "Berry", "Cherry"};
        List<String> list = Arrays.stream(array)
                                  .filter(s -> s.startsWith("B"))
                                  .collect(Collectors.toList());
        System.out.println("List: " + list);
    }
}
```

This outputs the following:

```
List: [Banana, Berry]
```

---

*Source: [sentry.io/answers/how-to-convert-an-array-to-a-list-in-java/](https://sentry.io/answers/how-to-convert-an-array-to-a-list-in-java/)*
