How to Create ArrayList from array in Java ?

In Java, you can create an ArrayList from an array using the Arrays.asList() method combined with the ArrayList constructor. Here’s how to do it:

Method 1: Using Arrays.asList()

This is the simplest and most common approach.

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

public class Main {
    public static void main(String[] args) {
        // Example array
        String[] array = {"Apple", "Banana", "Cherry"};

        // Convert array to ArrayList
        ArrayList<String> list = new ArrayList<>(Arrays.asList(array));

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

Explanation:

  • Arrays.asList(array) wraps the array into a fixed-size list backed by the array.
  • new ArrayList<>(...) converts the fixed-size list into a resizable ArrayList.

Method 2: Using Java 8 Streams (Flexible)

For more control (e.g., filtering/modifying elements):

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

public class Main {
    public static void main(String[] args) {
        String[] array = {"Apple", "Banana", "Cherry"};

        // Convert array to ArrayList using streams
        ArrayList<String> list = Arrays.stream(array)
                .collect(Collectors.toCollection(ArrayList::new));

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

Method 3: Manual Iteration (Legacy Approach)

For explicit control over element addition:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        String[] array = {"Apple", "Banana", "Cherry"};
        ArrayList<String> list = new ArrayList<>();

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

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

Key Notes:

  1. Primitive Arrays:
    Arrays.asList() works with object arrays (e.g., String[], Integer[]), not primitive arrays (e.g., int[]).
    For primitive arrays like int[], use a loop or Java 8 streams:
   int[] intArray = {1, 2, 3};
   ArrayList<Integer> intList = new ArrayList<>();
   for (int num : intArray) {
       intList.add(num); // Autoboxing to Integer
   }
  1. Mutability:
    The list from Arrays.asList() is fixed-size (you can’t add/remove elements). Use new ArrayList<>(Arrays.asList(...)) to create a mutable list.
  2. Shallow Copy:
    The ArrayList and the original array share references to the same objects. Changes to objects in one will reflect in the other.

Comparison

MethodProsCons
Arrays.asList()Simple, conciseFixed-size unless wrapped in ArrayList
Java 8 StreamsFlexible (filter/modify elements)Verbose for simple cases
Manual IterationWorks for primitive arraysMore code

When to Use Which:

  • Most cases: Use new ArrayList<>(Arrays.asList(array)).
  • Primitive arrays: Use manual iteration or streams.
  • Custom logic: Use streams (e.g., filtering elements while converting).

Leave a Reply

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