How to convert Array to List in Java

In this article, we will discuss how to convert Array to List in Java. So the first question arises in your mind is why you need to do such conversion. Well! many of the time when you call to some Java code which returns Array object and you need to pass it to another method which requires List object.

Recently while I invoked a web service request, it returned an array of string. Further, I need to pass an array of string to some other service but it required a list of string object i.e List<String> object. Hence, I had to convert the array of string object to a list of string object and pass it to service.

Let us understand various methods of converting the Array into List in Java.

Manual Conversion using for loop

To convert Array into List, one of the simplest ways is to loop the array to its size and add the Array element into List using add() method. Let us demonstrate it using below code snippet.

import java.util.ArrayList;
import java.util.List;

public final class ListToArrayDemo {
	public static void main(String[] args) {
		String[] strArray = new String[] { "Java", "Array", "List" };
		int strArrLength = strArray.length;
		List<String> lst = new ArrayList<String>();
		for (int count = 0; count < strArrLength; count++) {
			lst.add(strArray[count]);
		}
		System.out.println("***Displaying the results after converting Array to List***");
		for (String str : lst) {
			System.out.println(str);
		}
	}
}

The output of above code snippet is

***Displaying the results after converting Array to List***
Java
Array
List

Conversion using Arrays.asList() method

Using asList() method, you can convert Array to List in Java. asList() method is defined in Arrays class in Java.

Let us demonstrate how to convert Array to List in Java using asList() method.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public final class ListToArrayDemo {
	public static void main(String[] args) {
		String[] strArray = new String[] { "Java", "Array", "List" };
		List<String> lst = Arrays.asList(strArray);
		
		System.out.println("***Displaying the results after converting Array to List lst***");
		for (String str : lst) {
			System.out.println(str);
		}
		List<String> lst1 = new ArrayList<String>(Arrays.asList(strArray));
		lst1.add("String");
		System.out.println("***Displaying the results after converting Array to List lst1***");
		for (String str : lst1) {
			System.out.println(str);
		}
	}
}

The output of above code snippet is

***Displaying the results after converting Array to List lst***
Java
Array
List
***Displaying the results after converting Array to List lst1***
Java
Array
List
String

In the above example, we used Arrays.asList() method to convert Array to List. In above code snippet, we created two lists i.e lst and lst1

List<String> lst = Arrays.asList(strArray);

The list assigned to above list is a fixed-size list backed by the original array. Hence, you cannot add a new element to above List lst . If you try to add new element using add() method you will get following exception

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(Unknown Source)
	at java.util.AbstractList.add(Unknown Source)
	at ListToArrayDemo.main(ListToArrayDemo.java:10)

To support add() operation, you can use List<String> lst1 = new ArrayList<String>(Arrays.asList(strArray)); as described in above code snippet and you can see that using add() method new element has been added to lst1 object.

Conversion using Collections.addAll() method

Using Collections.addAll() method you can convert Array into List in java. This is one of the common method to convert Array into List.

Let us demonstrate how to convert Array to List using addAll() method in Java.

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class ListToArrayDemo {
	public static void main(String[] args) {
		String[] strArray = new String[] { "Java", "Array", "List" };
		List<String> lst = new ArrayList<String>();
		Collections.addAll(lst, strArray);
		System.out.println("***Displaying the results after converting Array to List lst***");
		for (String str : lst) {
			System.out.println(str);
		}
		
	}
}

The output of above code snippet is

***Displaying the results after converting Array to List lst***
Java
Array
List
Article tagged as
Author
Author: Amit Gupta
Published On: 11/09/2015
Last revised On: 13/11/2015
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media