In recent posts, we have seen how we can convert ArrayList to Array. In this post, we will see how we can convert array to list java or array to ArrayList.
There are a number of ways to convert array to list, we will discuss them.
Below is the list of topics this article covers.
1. Array to list in Java with Arrays.asList()
2. The Collections.addAll() method
3. Doing it manually
4. Using Java Streams
5. Using the List.of() method
Array to list in Java with Arrays.asList()
This is the most common approach for converting an array to a list in Java. However, it returns a fixed-size list, so you cannot add or remove elements. You can still change the value of an existing element.
public static List asList(T... a)
access modifier, it’s a public method so we can access it everywhere.
static, as it is a static method so it can access by class name.
return, it returns a fixed-size List as of size of the given array.
T, T represents the class of the array’s object.
The returned List has the same size and keeps the same element order as the original array.
import java.util.Arrays;
import java.util.List;
public class ArrayListExample
{
public static void main(String args[])
{
// Creating an ArrayList by Array
List<String> list = Arrays.asList("Java", "Goal", ".com", "Learning", "Website");
// We can't remove element from ArrayList
//list.remove(".com");
// We can change the element
list.set(2, "Java");
System.out.println("After changing element: "+ list);
// We can add more element in ArrayList
//list.add("Hi");
}
}
Output: After changing element: [Java, Goal, Java, Learning, Website]
Array to list in Java with Collections.addAll()
This method copies the given elements into the specified collection.
public static boolean addAll(Collection c, T... a)
access modifier: it is a public method, so it can be accessed from anywhere.
static: because it is static, it can be called using the class name.
return, it returns a boolean value.
T: T stands for the class of the objects
UnsupportedOperationException: thrown if collection c does not support the add method. IllegalArgumentException: thrown if, for some reason, the element cannot be added to collection c.
NullPointerException: thrown if collection c does not permit null values. Here collection c is the parameter that receives the elements supplied by parameter a
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListExample
{
public static void main(String args[])
{
String data[] = new String[] {"Java", "Goal", "Java", "Learning", "Website"};
ArrayList<String> list = new ArrayList<String>();
// Creating an ArrayList by Array
Collections.addAll(list,data);
System.out.println("ArrayList element: "+ list);
}
}
Output: ArrayList element: [Java, Goal, Java, Learning, Website]
Converting manually
You can also read each element from the array and add it to an ArrayList yourself.
import java.util.ArrayList;
public class ArrayListExample
{
public static void main(String args[])
{
ArrayList<String> arraylist= new ArrayList<String>();
String data[] = {"Java","Goal","Java","Learning", "Webiste"};
for(int i =0;i<data.length;i++)
arraylist.add(data[i]);
System.out.println("Elements from ArrayList: "+arraylist);
}
}
Output: Elements from ArrayList: [Java, Goal, Java, Learning, Webiste]
Using Java Streams
Java 8 introduced the concept of streams. We can use a stream to convert an array into an ArrayList.
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ArrayListExample
{
public static void main(String args[])
{
String data[] = {"Java","Goal","Java","Learning", "Webiste"};
ArrayList<String> list = Stream.of(data).collect(Collectors.toCollection(ArrayList::new));
System.out.println("Element from ArrayList: "+ list);
}
}
Output: Elements from ArrayList: [Java, Goal, Java, Learning, Webiste]
Using the List.of() method
This method was introduced in java 9. This method returns an unmodifiable list. It means we can’t add or remove data from it.
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample
{
public static void main(String args[])
{
String data[] = {"Java","Goal","Java","Learning", "Webiste"};
List<String> list = new ArrayList<>(List.of(data));
System.out.println("Element from ArrayList: "+ list);
}
}
Output: Elements from ArrayList: [Java, Goal, Java, Learning, Webiste]

