Site icon JavaGoal

How to convert array to list java

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.

Here is the table content of the article will we will cover this topic.
1. Array to list Java by Arrays.toList()
2. Collections.addAll() method
3. We can use the manual way
4. By use of Java Stream
5. By use of List.of() method

Array to list Java by Arrays.toList()

This is the most popular way to convert array to list java. But it returns fixed size List and we can’t add or remove elements from it. But we can change the value of the 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.

This method returns a List that has the same size and same order of elements as in 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 Java Collections.addAll() method

This method is used to copy the content of specified elements to specified collection.

public static  boolean addAll(Collection c, 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 boolean value.
T, T represents the class of object
UnsupportedOperationException, If collection c does not support add method IllegalArgumentException, if due to some reason, prevents the element added to collection c.
NullPointerException, if Collection c doesn’t permit any null value. Here collection C is the parameter that takes 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]

We can use manual way

We can get each element from Array and store it in ArrayList by manual way.

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]

By use of Java Stream

In java 8, we have a concept streams in java. We can use the stream and convert an Array to 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]

By use of 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]

Exit mobile version