Java
Programming
Data Structures
Array to List Conversion
Coding Tutorial

Converting array to list in Java

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting an array to a list in Java is easy, but the details matter because not all resulting lists behave the same way. Some conversions produce a fixed-size view backed by the original array, while others produce a fully mutable list copy.

The right approach depends on what you need next: read-only access, mutation, stream processing, or support for primitive arrays.

Arrays.asList for the Basic Case

The classic conversion is Arrays.asList(array).

java
1import java.util.Arrays;
2import java.util.List;
3
4public class Example {
5    public static void main(String[] args) {
6        String[] array = {"apple", "banana", "cherry"};
7        List<String> list = Arrays.asList(array);
8        System.out.println(list);
9    }
10}

This is concise and efficient, but the result has an important limitation: it is fixed in size.

You can replace an element with set, but you cannot add or remove elements.

java
list.set(1, "blueberry");
// list.add("date");  // UnsupportedOperationException

It is also backed by the original array, which means updates can be reflected both ways.

Make a Fully Mutable List Copy

If you need to add or remove elements, wrap the result in an ArrayList.

java
1import java.util.ArrayList;
2import java.util.Arrays;
3import java.util.List;
4
5public class MutableListExample {
6    public static void main(String[] args) {
7        String[] array = {"apple", "banana", "cherry"};
8        List<String> list = new ArrayList<>(Arrays.asList(array));
9
10        list.add("date");
11        System.out.println(list);
12    }
13}

This is the most common conversion when the list will be edited after creation.

Stream-Based Conversion

Streams are useful when you want to transform values while converting.

java
1import java.util.Arrays;
2import java.util.List;
3import java.util.stream.Collectors;
4
5public class StreamExample {
6    public static void main(String[] args) {
7        String[] array = {" apple ", " banana ", " cherry "};
8
9        List<String> list = Arrays.stream(array)
10                .map(String::trim)
11                .map(String::toUpperCase)
12                .collect(Collectors.toList());
13
14        System.out.println(list);
15    }
16}

That is more verbose than Arrays.asList, but it is better when conversion and transformation belong together.

Primitive Arrays Are Different

A very common trap is expecting Arrays.asList to work the same way for int[] or double[].

java
int[] numbers = {1, 2, 3};
List<int[]> wrong = Arrays.asList(numbers);

That produces a list with one element: the entire primitive array.

For primitive arrays, use streams and boxing.

java
1import java.util.Arrays;
2import java.util.List;
3import java.util.stream.Collectors;
4
5int[] numbers = {1, 2, 3};
6List<Integer> list = Arrays.stream(numbers)
7        .boxed()
8        .collect(Collectors.toList());
9
10System.out.println(list);

This is the correct way to get List<Integer> from int[].

Modern Read-Only Alternatives

If you want an unmodifiable list from values you already know, List.of(...) is often cleaner than array conversion, though it starts from individual elements rather than from an existing array.

If you already have an array and want an immutable snapshot, use:

java
List<String> list = List.copyOf(Arrays.asList(array));

That creates an unmodifiable list independent of later structural changes.

Common Pitfalls

The biggest mistake is assuming Arrays.asList returns a normal fully mutable ArrayList. It does not.

Another common issue is forgetting that the returned list is backed by the original array. Mutating one can affect the other.

Primitive arrays are another frequent source of confusion. Arrays.asList(intArray) does not give you List<Integer>.

Finally, use streams when you genuinely need transformation logic, not just because streams exist. For simple object-array conversion, Arrays.asList or new ArrayList<>(...) is usually clearer.

Summary

  • 'Arrays.asList(array) is the standard object-array to list conversion.'
  • The result is fixed-size and backed by the original array.
  • Wrap it in new ArrayList<>(...) if you need a mutable list.
  • Use streams when you want to transform elements during conversion.
  • Primitive arrays need boxing, not plain Arrays.asList.
  • Choose the conversion style based on mutability and data type.

Course illustration
Course illustration

All Rights Reserved.