Signup/Sign In
LAST UPDATED: JANUARY 22, 2021

Java Arrays asList() Method

In this tutorial, we will learn about asList() method from the Arrays class located into java.util package with some intuitive examples. This method acts as a channel between array-based and collection-based API. It is mostly used to create list from array of any type.

Syntax:

public static List asList(T... arr)

This method receives an array of wrapper class as a parameter and then returns a list of array elements. In a case array is empty it will return NullPointerException.

Example: Get List from String Array

In the example below, we have an array of some Strings. After calling Arrays.asList() we are assigning it to List<String> list it means our array is converted to List<String> and this is the way asList() method works.

import java.util.Arrays;
import java.util.List;
public class StudyTonight 
{
	public static void main(String[] args)  
	{
		String arr[]= new String[] { "Java", "C", "C++", "Python" };
		List<String> list = Arrays.asList(arr);
		System.out.println("List: " + list);
	}
}


List: [Java, C, C++, Python]

Example: Get List from Integer Array

Similar to the above example here we used Integer class. In the code given below, we are converting an array of Integer class to a list of integers.

import java.util.Arrays;
import java.util.List;
public class StudyTonight 
{
	public static void main(String[] args)  
	{
		Integer arr[]= new Integer[] { 12, 34, 22, 87, 98 };
		List<Integer> list = Arrays.asList(arr);
		System.out.println("List of Integers: " + list);
	}
}


List: [12, 34, 22, 87, 98]

Example: Get List from Several Type of Arrays

In this example, we have several arrays of type float, byte, short, etc and get list of each type by using the asList() method of Arrays class.

import java.util.Arrays;
import java.util.List;
public class Main 
{
	public static void main(String[] args)  
	{
		Float arr1[]= new Float[] { 12.5f, 34.2f, 22.22f, 87.1f, 98.3f };
		List<Float> list1 = Arrays.asList(arr1);
		System.out.println("List of Integers: " + list1);
		
		Byte arr2[]= new Byte[] { 12, 34, 22, 87, 98 };
		List<Byte> list2 = Arrays.asList(arr2);
		System.out.println("List of Integers: " + list2);
		
		Boolean arr3[]= new Boolean[] { true, false, false};
		List<Boolean> list3 = Arrays.asList(arr3);
		System.out.println("List of Integers: " + list3);
		
		Short arr4[]= new Short[] { 12, 34, 22, 87, 98 };
		List<Short> list4 = Arrays.asList(arr4);
		System.out.println("List of Integers: " + list4);
	}
}


List of Integers: [12.5, 34.2, 22.22, 87.1, 98.3]
List of Integers: [12, 34, 22, 87, 98]
List of Integers: [true, false, false]
List of Integers: [12, 34, 22, 87, 98]

enlightened This method works only of wrapper classes array in java like Integer, Strings and etc. otherwise it will return an unwanted list type which can cause a problem later.

Conclusion:

The asList() method is used to convert array to list of elements. This method accepts an array and returns a list of that specified array. This method can be called without creating an object as this is the static method we can directly call it over a class name.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.