Sentry Answers>Java>

How to convert an array to a List in Java?

How to convert an array to a List in Java?

Abdul D.

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.

Click to Copy
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.

Click to Copy
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.

Click to Copy
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:

Click to Copy
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.

Click to Copy
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:

Click to Copy
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:

Click to Copy
List: [Banana, Berry]
  • Sentry BlogException Handling in Java (with Real Examples)
  • Syntax.fmListen to the Syntax Podcast
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark of Functional Software, Inc.