Table of Contents

Converting an array to a list in Java is a common requirement, whether you’re working with collections, processing data, or leveraging Java’s Stream API. In Java 8, there are multiple ways to achieve this, including Arrays.asList(), Stream.of(), and Arrays.stream() with Collectors.toList().

Each method has its own advantages and limitations—some create fixed-size lists, while others generate fully mutable lists. Additionally, handling primitive arrays requires a different approach.

In this guide, we’ll explore the best methods to convert an array to a list in Java 8, compare their performance and usability, and provide clear code examples to help you choose the right approach for your needs.

1. Using Arrays.asList() – Fixed-Size List

Example

import java.util.Arrays;
import java.util.List;

public class ArraysAsListExample {
    public static void main(String[] args) {
        String[] array = {"Apple", "Banana", "Cherry"};
        List<String> list = Arrays.asList(array);

        System.out.println(list); // Output: [Apple, Banana, Cherry]

        list.set(1, "Blueberry"); // Allowed: Modifying elements
        System.out.println(list); // Output: [Apple, Blueberry, Cherry]

        // list.add("Mango"); // Throws UnsupportedOperationException
        // list.remove("Apple"); // Throws UnsupportedOperationException
    }
}

Key Characteristics:

Fast conversion – Works in O(1) time since it directly wraps the array.
Allows modifying elements using set().
Fixed size – Cannot add or remove elements.
Modifying the original array affects the list (they share the same backing array).

2. Using Stream.of() with Collectors.toList() – Mutable List

Example:

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamToListExample {
    public static void main(String[] args) {
        String[] array = {"Apple", "Banana", "Cherry"};
        List<String> list = Stream.of(array)
                                  .collect(Collectors.toList());

        System.out.println(list); // Output: [Apple, Banana, Cherry]

        list.add("Mango"); // Allowed
        list.remove("Apple"); // Allowed
        System.out.println(list); // Output: [Banana, Cherry, Mango]
    }
}

Key Characteristics:

Returns a new ArrayList (mutable).
Allows adding/removing elements.
Independent from the original array (changes do not reflect in the array).
Slightly slower than Arrays.asList() because it creates a new list.

3. Using Arrays.stream() + Collectors.toList() – Best for Primitives

Example:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class PrimitiveArrayToList {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        List<Integer> list = Arrays.stream(numbers)  // Converts int[] to IntStream
                                   .boxed()         // Converts IntStream to Stream<Integer>
                                   .collect(Collectors.toList());

        System.out.println(list); // Output: [1, 2, 3, 4, 5]
    }
}

Key Characteristics:

Works with primitive arrays (int[], double[], etc.).
Returns a mutable list.
Independent from the original array.
Needs boxed() to convert primitive stream to Stream<Integer>.

4. Comparison Table: Best Method to Convert Array to List in Java 8

FeatureArrays.asList()Stream.of().collect(Collectors.toList())Arrays.stream().collect(Collectors.toList())
Mutability❌ Fixed-size✅ Fully mutable✅ Fully mutable
Performance✅ Fast (O(1))❌ Slightly slower (O(n))❌ Slightly slower (O(n))
Allows add/remove?❌ No✅ Yes✅ Yes
Works with primitives?❌ No (autoboxing issue)❌ No (requires boxed())✅ Yes (with boxed())
Independent from array?❌ No (shared backing array)✅ Yes (new list)✅ Yes (new list)

5. When to Use Each Method?

Use Arrays.asList() when you need a fixed-size list and don’t require adding/removing elements.
Use Stream.of().collect(Collectors.toList()) when you need a mutable list.
Use Arrays.stream().collect(Collectors.toList()) when working with primitive arrays.

6. Conclusion

Converting an array to a list in Java 8 is easy, but choosing the right method depends on your use case.

  • If you need a fixed-size list, use Arrays.asList().
  • If you need a fully mutable list, use Stream.of() with Collectors.toList().
  • If you’re dealing with primitive arrays, use Arrays.stream() with boxed().

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Categorized in:

Java 8,