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.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java List

  • List vs. Array in Java
  • Sort Manual Linked List With Bubble Sort Algorithm in Java
  • Convert Collection to List in Java
  • Convert Map Values Into a List in Java
  • Related Article - Java ArrayList

  • List vs. Array in Java
  • Sort Manual Linked List With Bubble Sort Algorithm in Java
  • Convert Collection to List in Java
  • Convert Map Values Into a List in Java