Visit Sponsor

Written by 3:07 pm Core Java

Convert Array to List in Java – Methods and Examples

Converting an array to a List is a common requirement in Java development — whether you are adapting legacy code, collecting API results, or working with collections APIs that expect List types. Java offers multiple ways to perform this conversion depending on the data type, desired mutability, and target use case.

This tutorial explains all major approaches, their behavior, and best practices with clear code examples.

1. Using Arrays.asList()

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

Example

String[] array = {"Java", "Python", "C++"};
List<String> list = Arrays.asList(array);
System.out.println(list);

Key Characteristics:

  • Returns a fixed-size list backed by the original array
  • You cannot add or remove elements (UnsupportedOperationException)
  • Modifications to the list update the underlying array

When to Use

Use this when you need a view of the array as a list without structural changes.

2. Creating a Modifiable List

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

String[] array = {"Java", "Python", "C++"};
List<String> list = new ArrayList<>(Arrays.asList(array));
list.add("Go");
System.out.println(list);

This produces:

[Java, Python, C++, Go]

This approach creates a separate, modifiable list that does not affect the original array.

3. Using List.of() (Immutable List)

Java 9 introduced List.of() for quick immutable list creation:

String[] array = {"Java", "Python", "C++"};
List<String> list = List.of(array);

Notes:

  • Returns an immutable list
  • Attempts to modify (add/remove) will throw UnsupportedOperationException
  • Better semantic clarity when you do not intend to mutate the list

4. Using Java Streams (Java 8+)

Java Streams provide a fluent and functional conversion:

String[] array = {"Java", "Python", "C++"};
List<String> list = Arrays.stream(array)
                          .collect(Collectors.toList());

Better suited when:

  • You plan to apply filtering, mapping, or other transformations
  • You want a new mutable list as a result

Example with transformation:

List<String> upperCaseList = Arrays.stream(array)
                                   .map(String::toUpperCase)
                                   .collect(Collectors.toList());

5. Converting Primitive Arrays

Methods like Arrays.asList() do not work as expected with primitive arrays (e.g., int[]). Instead, the primitive array is treated as a single object.

Incorrect Example

int[] array = {1, 2, 3};
List<int[]> list = Arrays.asList(array);

This produces a list with one element (int[]) — not the values 1, 2, 3.

Correct Conversion

Use IntStream to box primitive values:

int[] array = {1, 2, 3};
List<Integer> list = IntStream.of(array)
                              .boxed()
                              .collect(Collectors.toList());

Use similar patterns for other primitives like long[] or double[].

6. Manual Loop Conversion

For full control or custom logic, convert using a loop:

String[] array = {"Java", "Python", "C++"};
List<String> list = new ArrayList<>();
for (String s : array) {
    if (s != null && !s.isBlank()) {
        list.add(s);
    }
}

This approach enables validation or filtering during conversion.

7. Conversion with Utility Libraries

Popular third-party libraries provide convenience methods:

Guava

String[] array = {"Java", "Python", "C++"};
List<String> list = Lists.newArrayList(array);

Apache Commons

String[] array = {"Java", "Python", "C++"};
List<String> list = new ArrayList<>(Arrays.asList(array));

These utilities can make code more expressive, especially in large codebases.

Mutability Considerations

Conversion MethodMutable ListBacked by Array
Arrays.asList()NoYes
List.of()NoNo
Stream + CollectYesNo
New ArrayList(…)YesNo
Manual LoopYesNo

Choose based on whether you need to add, remove, or change values.

Performance Notes

  • Arrays.asList() is fast and memory-efficient because it wraps the array
  • Streams and manual loops create new collections, which cost more memory
  • For large arrays, prefer collectors and buffering strategies to manage performance

Best Practices (Senior Engineering Insight)

  • Use immutable lists when data should not change
  • Wrap with ArrayList when you need mutability
  • Use Streams for transformation and functional pipelines
  • Always handle primitive arrays with streams and boxing

These practices produce safer, more maintainable Java code.

Summary

Converting an array to a List in Java is straightforward but requires careful choice of method based on mutability and performance needs:

  • Arrays.asList() — Quick, fixed-size view
  • new ArrayList<>(...) — Modifiable collection
  • List.of() — Immutable list
  • Streams — Flexible and powerful
  • Manual loop — Custom conversion logic

Each approach has its place depending on requirements.

Visited 7 times, 1 visit(s) today
Close