Arrays.asList in Java Examples

We generally use Arrays.asList method to copy an object array into a List object. It returns a fixed-size List object from the object array that you have passed to this method.

Since Arrays.asList method returns a fixed-size List object, you must remember that you cannot add or remove elements from this List object as you would do normally by calling the add method and remove method respectively. If you attempt to do so, it will throw UnsupportedOperationException which I’ve explained in one of my previous posts.

Method Signature

@SafeVarargs
public static <T> List<T> asList(T... a)

Arrays.asList method is a static method, so you don’t have to create an instance of Arrays class to call it, which is true for most of the Arrays class.

The return type of this method is List object. That is, if you pass a String array, you would get a List of String objects (List<String>), if you pass an array of Employee objects, the return object would be again a List of Employee objects (List<Employee>).

The argument can be any object array, but not primitive array. Though it doesn’t throw any error, even if you pass primitive array, but the returned List object just points to the array reference, which is of no use.

But you need to note that asList allow you to pass an array of integer literals which will be implicitly treated as Integer[].

If we can’t add or remove elements why to create List using asList method?

Since, asList returns a List object which is basically a collection object bundled with various methods, which are not generally available with an array. You can leverage the iterator method of List class to traverse through the List, whereas with an array you would have been ended up using more complicated for loop.

Converting an Object Array to List Example

You can take any Object array, be it a wrapper class or your custom class or any of the built-in classes which make sense to store in an array. You can pass that object array to Arrays.asList method as shown in this example.

package com.techstackjournal;

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

public class ArraysExample {

	public static void main(String[] args) {

		Integer[] arr = { 1, 2, 3, 4, 5 };

		List list = Arrays.asList(arr);

		for (Object object : list) {
			System.out.println(object);
		}

	}

}

Converting Array of Literals Example

In this example, we are not passing any array object to asList method, instead we are passing a list of literal values to it. Even with this approach you won’t be able to add any more values to the returned List, cause it is a fixed-size List object.

package com.techstackjournal;

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

public class ArraysExample {

	public static void main(String[] args) {

		List list = Arrays.asList(1, 2, 3, 4, 5);

		for (Object object : list) {
			System.out.println(object);
		}

	}

}

Result of Attempting to Convert Primitive Array using asList

package com.techstackjournal;

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

public class ArraysExample {

	public static void main(String[] args) {

		int arr[] = { 1, 2, 3 };
		List list = Arrays.asList(arr);
		System.out.println(list.size());
		for (Object object : list) {
			System.out.println(object);
		}

	}

}

Output:

1
[I@39ed3c8d