Convert List to ArrayList in Java

Haider Ali Nov 08, 2021 Oct 06, 2021 Java Java List Java ArrayList
Convert List to ArrayList in Java

In this guide, we have talked about how you can convert a list to an ArrayList in Java. But before we go into it, you should be familiar with some of the basic concepts in Java. You need to understand that list is implemented by the Interface Collection, and ArrayList is an implemented class of List.

Converting List to ArrayList in Java

Let’s take a look at the example down below.

import java.util.*;
public class Hello 
{
    public static void main(String[] args)
    {
        //Let's make a List first. 
        List<String>  MyList = (List<String>) Arrays.asList("Hello","World");
    }
}

The above List contains two string elements, as you can see. Here, Arrays.asList is a static method used to convert an array of objects into a List. Let’s see how we can have this List convert into an ArrayList.

Learn more about Array Class here.

import java.util.*;
public class Hello 
{
    public static void main(String[] args)
    {
        //Let's make a List first. 
        List<String>  MyList = (List<String>) Arrays.asList("Hello","World");
        ArrayList<String> a1 = new ArrayList<String>(MyList);
    }
}

With this approach, we are actually initializing the ArrayList featuring its predefined values. We simply made a list with two elements using the Arrays.asList static method. Later we used the constructor of the ArrayList and instantiated it with predefined values. Learn more about ArrayList and its methods and other properties.

In other words, we had an array with elements in it and converted it into List and later turned that list into an ArrayList. Take a look at the example down below for understanding what’s happening.

import java.util.*;
public class Hello 
{
    public static void main(String[] args)
    {
        String arr[]={"1","2","3"};
        List<String>  MyList = (List<String>) Arrays.asList(arr);

        //now we are converting list into arraylist
        ArrayList<String> a1 = new ArrayList<String>(MyList);
        for(int i=0; i<a1.size(); i++)
        {
            System.out.println(a1.get(i));
        }
    }
}

In the above program, we first made an Array with initializing values. Later, just like in the first example, instead of giving values, we passed an array, and we used Arrays.asList to convert this array of objects into a List.

The list that you get from Arrays.asList is not modifiable. It’s just a wrapper, and you can’t add or remove over it. Even if you try, you will get

UnsupportedOperationException

The problem here is to convert the list into an ArrayList, so we instantiated the ArrayList from the List. The output of the above program:

1
2
3

That’s how you convert the List into an ArrayList in Java.

Author: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn

Related Article - Java List

  • Split a List Into Chunks in Java
  • Get First Element From List in Java
  • Find the Index of an Element in a List Using Java
  • Filter List in Java
  • Differences Between List and Arraylist in Java
  • List vs. Array in Java
  • Related Article - Java ArrayList

  • Sort Objects in ArrayList by Date in Java
  • Find Unique Values in Java ArrayList
  • Vector and ArrayList in Java
  • Differences Between List and Arraylist in Java
  • Compare ArrayLists in Java
  • Merge Sort Using ArrayList in Java