Fundamentals 8 min read

Comparing Three Ways to Convert Java Arrays to List and Their Use Cases

This article compares three common methods for converting Java arrays to List—Arrays.asList, using an ArrayList constructor, and Collections.addAll—detailing their advantages, limitations, performance, and appropriate scenarios, while also explaining common type conversion errors and best practices for primitive and wrapper types.

Java Captain
Java Captain
Java Captain
Comparing Three Ways to Convert Java Arrays to List and Their Use Cases

The article introduces three ways to convert a Java array to a List and analyzes their pros, cons, and suitable application scenarios.

1. Most common method (not always optimal)

Using Arrays.asList(strArray) converts an array to a fixed-size list; attempts to add or remove elements throw UnsupportedOperationException . Example code:

private void testArrayCastToListError() {
  String[] strArray = new String[2];
  List list = Arrays.asList(strArray);
  // Attempt to insert a value
  list.add("1");
  System.out.println(list);
}

Running this code results in an UnsupportedOperationException because the returned list is an instance of the private static inner class java.util.Arrays.ArrayList , which lacks add() and remove() methods.

Use case: suitable when the list is read‑only and only serves as a data source.

2. Convert array to a mutable list

Wrap the result of Arrays.asList(strArray) with a new ArrayList to obtain a fully mutable list:

ArrayList
list = new ArrayList
(Arrays.asList(strArray));
list.add("1");
System.out.println(list);

The execution prints [null, null, 1] , demonstrating successful addition. Use case: when the array size is small and you need to perform add/remove operations.

3. Use Collections.addAll() (most efficient)

Creating an ArrayList with the desired capacity and filling it via Collections.addAll() is the most efficient for large data sets:

ArrayList
arrayList = new ArrayList
(strArray.length);
Collections.addAll(arrayList, strArray);
arrayList.add("1");
System.out.println(arrayList);

This also prints [null, null, 1] . The source of Collections.addAll() is included for reference.

Q&A

Q: Will converting an int[] to a List cause errors? A: In JDK 1.8 the three methods work for Integer[] . For primitive int[] , Arrays.asList(intArray) returns a List , not a List , leading to a compile‑time type mismatch.

Key points:

Generics in Java require reference types; primitive types cannot be used as type arguments.

Therefore, only wrapper types like Integer can be converted to List .

Other reference types (e.g., String , arrays, classes) can be used as generic arguments.

Summary

Understanding why int[] cannot be directly converted to List and why Integer[] can helps avoid common pitfalls. By examining JDK source code, you gain both practical usage knowledge and the underlying reasoning.

JavaCollectionsarrayArrays.asListlistArrayListCollections.addAll
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.