How to convert array to list in Java ?

Here’s a detailed guide on converting arrays to lists in Java, including various methods, use cases, and code examples:

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

Best For: Read-only operations or when you need a list backed by the original array.
Limitation: List size cannot be modified (throws UnsupportedOperationException).

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

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

        // Integer array (object type)
        Integer[] intArray = {1, 2, 3};
        List<Integer> intList = Arrays.asList(intArray);

        System.out.println(strList); // [Apple, Banana, Cherry]
        System.out.println(intList);  // [1, 2, 3]
    }
}

Pitfall:

strList.add("Date"); // ❌ Throws UnsupportedOperationException

2. Create a Modifiable List with new ArrayList<>()

Best For: Creating an independent, mutable list from an array.
Works With: Object arrays (e.g., String[], Integer[]).

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

public class Main {
    public static void main(String[] args) {
        String[] array = {"A", "B", "C"};
        List<String> list = new ArrayList<>(Arrays.asList(array));

        list.add("D"); // ✅ Works
        list.remove(0); // ✅ Works
        System.out.println(list); // [B, C, D]
    }
}

3. Java 8+ Streams for Primitive Arrays

Best For: Converting primitive arrays (e.g., int[], double[]) to lists.

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

public class Main {
    public static void main(String[] args) {
        // int[] to List<Integer>
        int[] intArray = {1, 2, 3};
        List<Integer> intList = Arrays.stream(intArray)
                                      .boxed()
                                      .collect(Collectors.toList());

        // double[] to List<Double>
        double[] doubleArray = {1.1, 2.2, 3.3};
        List<Double> doubleList = Arrays.stream(doubleArray)
                                        .boxed()
                                        .collect(Collectors.toList());

        System.out.println(intList);    // [1, 2, 3]
        System.out.println(doubleList); // [1.1, 2.2, 3.3]
    }
}

4. Manual Conversion (For Full Control)

Best For: Older Java versions (< Java 8) or explicit element handling.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        String[] array = {"X", "Y", "Z"};
        List<String> list = new ArrayList<>();

        for (String element : array) {
            list.add(element);
        }

        System.out.println(list); // [X, Y, Z]
    }
}

5. Using List.of() (Java 9+)

Best For: Immutable lists.
Works With: Java 9 and newer.

import java.util.List;

public class Main {
    public static void main(String[] args) {
        String[] array = {"One", "Two", "Three"};
        List<String> list = List.of(array); // Immutable list

        System.out.println(list); // [One, Two, Three]
        list.add("Four"); // ❌ Throws UnsupportedOperationException
    }
}

Comparison Table

MethodMutabilityPrimitive SupportJava VersionNotes
Arrays.asList()❌ (objects only)1.2+Backed by the original array.
new ArrayList<>(Arrays.asList())❌ (objects only)1.2+Independent mutable list.
Java 8 Streams✅ (with boxing)8+Best for primitives.
Manual Loop1.2+Verbose but explicit.
List.of()❌ (objects only)9+Immutable list.

Common Scenarios

Scenario 1: Convert int[] to List<Integer>

int[] numbers = {10, 20, 30};
List<Integer> numberList = Arrays.stream(numbers)
                                 .boxed()
                                 .collect(Collectors.toList());

Scenario 2: Convert char[] to List<Character>

char[] chars = {'a', 'b', 'c'};
List<Character> charList = new ArrayList<>();
for (char c : chars) {
    charList.add(c);
}

Edge Cases

Pitfall 1: Primitive Arrays with Arrays.asList()

int[] array = {1, 2, 3};
List<int[]> wrongList = Arrays.asList(array); // List contains a single int[] element!
System.out.println(wrongList.get(0).length); // 3 (array length)

Pitfall 2: Empty Arrays

String[] emptyArray = {};
List<String> emptyList = Arrays.asList(emptyArray); // Empty fixed-size list

Best Practices

  1. Use Arrays.stream() for primitive arrays.
  2. Prefer new ArrayList<>(Arrays.asList()) for mutable lists.
  3. Avoid modifying Arrays.asList() lists.
  4. Use List.of() for immutable lists (Java 9+).

Full Example Workflow

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

public class ArrayToListDemo {
    public static void main(String[] args) {
        // Example 1: Fixed-size list
        String[] fruits = {"Apple", "Mango"};
        List<String> fixedList = Arrays.asList(fruits);
        System.out.println("Fixed List: " + fixedList);

        // Example 2: Mutable list
        List<String> mutableList = new ArrayList<>(fixedList);
        mutableList.add("Banana");
        System.out.println("Mutable List: " + mutableList);

        // Example 3: Primitive array to list
        int[] primes = {2, 3, 5, 7};
        List<Integer> primeList = Arrays.stream(primes)
                                        .boxed()
                                        .collect(Collectors.toList());
        System.out.println("Prime List: " + primeList);
    }
}

Output:

Fixed List: [Apple, Mango]
Mutable List: [Apple, Mango, Banana]
Prime List: [2, 3, 5, 7]

This covers all major approaches to convert arrays to lists in Java, along with edge cases and best practices! Let me know if you need further clarification.

Leave a Reply

Your email address will not be published. Required fields are marked *