Abdul D.
—I don’t know how to convert an array to a List
in Java.
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:
Arrays.asList()
methodArrayList
constructorCollections.addAll()
methodStream
APIArrays.asList()
The Arrays.asList()
method is the simplest way to convert an array to a List
.
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
.
ArrayList
The result of the Arrays.asList()
method can then be passed to the ArrayList
constructor to create a mutable list.
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.
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.
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]
If you are using Java 8 or later, you can use the Stream
API to convert an array to a List
.
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:
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]
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “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.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.