Java Convert Array to List

In this post, we will see how to convert array to list in java.

There are may ways to convert array to list:

[toc]

By using Arrays.asList():

This is an easy method to convert Array into list.We just need to pass array as parameter to asList() method.It is a Static method available in Arrays class so, called it by class name Arrays.asList().

Syntax:

public static List asList(T x)

It returns a fixed size List as of size of given array.Since returned List is fixed-size we cannot add more elements.If we do so it throws UnsupportedOperationException And T represent generics means any type of Array.

Example:

package org.arpit.java2blog;
//import all these package for conversion
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
public class ConvertArrayToListAsListMain
{
    public static void main(String arg[])
    {
        //create and initialization of an array
        Integer[] arr={81,82,83,84,85};

        //print content of Array
        System.out.println("Elements of Array are:");
        for (int a: arr)
        {
            System.out.println(a);
        }

        //Convert array into list using  Arrays.asList() method 
        List<Integer> list=Arrays.asList(arr);
        System.out.println("Elements of list are:");
        //print elements of list
        System.out.println(list);
    }
}

OUTPUT:

Elements of Array are:
[81]
[82]
[83]
[84]
[85]
Elements of list are: [81,82,83,84,85]

Using Collections.addAll() method :

This method is available in Collections class.Remember that the array and List should be of same type.

Syntax:

public static boolean addAll(Collection c,T x)

Where c is a collection into which value is to be insert and x is an array contains values to be insert in c.

package org.arpit.java2blog;
//import all these package for conversion
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

public class ConvertArrayToListAsListMain
{
    public static void main(String arg[])
    {
        //creation of Integer array
        Integer[] arr={81,82,83,84,85};
        //print content of Array
        System.out.println("Elements of Array are:");
        for (int a: arr)
        {
            System.out.println(a);
        }
        //creation of List
        List<Integer> list=new ArrayList<>();
        //assign all the element of array in list using Collection.addAll() method 
        Collections.addAll(list,arr);
        System.out.println("Elements of list are:");
        //print elements of list
        System.out.println(list);
    }
}

OUTPUT:

Elements of Array are:
[81]
[82]
[83]
[84]
[85]
Elements of list are: [81,82,83,84,85]

Using Java 8’s Stream

We can use Java 8’s Stream to convert an array to List.

package org.arpit.java2blog;
//import all these package for conversion
import java.util.List;
import java.util.stream.Collectors;
import java.util.Arrays;

public class ConvertArrayToListAsListMain
{
    public static void main(String arg[])
    {
        //creation of Integer array
        int[] arr={1,3,5,6,10};
        //print content of Array
        System.out.println("Elements of Array are:");
        for (int a: arr)
        {
            System.out.println(a);
        }
        //Using Java 8 Stream
        List<Integer> list=Arrays.stream(arr).boxed().collect(Collectors.toList());

        System.out.println("Elements of list are:");
        //print elements of list
        System.out.println(list);
    }
}

OUTPUT:

Elements of Array are:
1
3
5
6
10
Elements of list are: [1,3,5,6,10]

Manual way using add() method:

We use add() method inside loop to convert Array into List.
Example :

package org.arpit.java2blog;
import java.util.List;
import java.util.ArrayList;
public class ConvertArrayToListMain
{
    public static void main(String arg[])
    {
        //create and initialization of array
        int[] arr={81,82,83,84,85};

        //print content of  Array using for each loop
        System.out.println("Elements of Array are:");
        for (int a: arr)
        {
            System.out.println(a);
        }
        List<Integer> list=new ArrayList<>();
        //conversion of array into list using add() method 
        for (int a: arr)
        {
            list.add(a);
        }
        System.out.println("Elements of list are:");
        System.out.println(list);
    }
}

OUTPUT:

Elements of Array are:
[81]
[82]
[83]
[84]
[85]
Elements of list are: [81,82,83,84,85]

That’s all about Converting the array to List in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.