In this post, we will see how to convert array to list in java.
There are may ways to convert array to list:
Table of Contents
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:
1 2 3 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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:
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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:
[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.