Help Save Code2care! 😢

I've lost 99% of my revenue to AdBlockers & AI. Your support could be the lifeline that keeps this passion project alive!

Buy Code2care a Coffee QR Code

Scan to Buy Me A Coffee and help me continue coding for you!

Converting Array into ArrayList - Java


There are serval ways in which we can convert Array into ArrayList in Java, let's take a look at a few examples.


Example 1: Arrays.asList()

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

public class ArrayToArrayListExample1 {

    public static void main(String[] args) {

        String[] arrayOfStrings = {"A", "B", "C", "D", "E"};
        

        List<String> list = Arrays.asList(arrayOfStrings);
        
        ArrayList<String> arrayList = new ArrayList<>(list);
        
        System.out.println(arrayList);
    }
}
Output:

[A, B, C, D, E]


Example 2: Using Loops

import java.util.ArrayList;

public class ArrayToArrayListExample2 {

    public static void main(String[] args) {
        String[] arrayOfStrings = {"A", "B", "C", "D", "E"};
        

        ArrayList<String> arrayList = new ArrayList<>();
        

        for (String element : arrayOfStrings) {
            arrayList.add(element);
        }
        
        System.out.println(arrayList);
    }
}

Example 3: Using Java 8 Stream API

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

public class ArrayToArrayListExample3 {

    public static void main(String[] args) {

        String[] arrayOfStrings = {"A", "B", "C", "D", "E"};
        

        List<String> list = Arrays.stream(arrayOfStrings)
                                  .collect(Collectors.toList());
        

        ArrayList<String> arrayList = new ArrayList<>(list);
        
        System.out.println(arrayList); 
    }
}

Example 4: Collections.addAll()

import java.util.ArrayList;
import java.util.Collections;

public class ArrayToArrayListExample4 {

    public static void main(String[] args) {

        Integer[] arrayOfIntegers = {1, 2, 3, 4, 5};
        
        ArrayList arrayList = new ArrayList<>();
        
        Collections.addAll(arrayList, arrayOfIntegers);
        
        System.out.println(arrayList);
    }
}
Output:

[1, 2, 3, 4, 5]


Conclusion:

    Use Arrays.asList() for quick, fixed-size list conversion; use a loop for element processing or large arrays; use Collections.addAll() for adding elements to an existing ArrayList; and use the Stream API for functional programming and additional processing in Java 8 and later.

You can download this article in various formats for your convenience. Choose from the options below:

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has a Masters Degree in Computer Science with over 15+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright Code2care © 2024 | Privacy Policy | About Us | Contact Us | Search