Tutorial: Learn how to use the java.util.Arrays asList method in Java now! 🤔
In this Java 8 instructional we’ll cover how to use the Arrays.asList method in Java with a step-by-step example that demonstrates how this method works and also explains why the instance of java.util.List returned is immutable.
Java Arrays.asList Tutorial TOC
See also the parent article regarding how to convert an Array to an ArrayList in Java and also be careful not to get burned by performance issues as it pertains to autoboxing in Java.
On the right is a link to the GitHub Gist for the step-by-step review covered in the next section.
As per the documentation for the Arrays class, the Arrays.asList array method returns a fixed-size list backed by the specified array and does not support the add method or other list modification methods; attempts to call any mutation operations will result in an exception being thrown.
If you need a mutable list then see the follow-up tutorial that covers how to use the ArrayList constructor in Java.
Let’s get right into an example and walk through what’s happening in each step.
This article was updated on March 13, 2025.
Do you need an Interim CTO? Let’s Talk!
Java Arrays asList Example
In this step-by-step walkthrough we’ll go over how to use the Arrays.asList method and discuss the instance of List which it returns and why it doesn’t support all of the same operations as the java.util.ArrayList.
We can run the Java Arrays asList example directly from our browser using the tio.run online interpreter.
This Groovy code example relies on Java 8 API and it should be easy enough to convert this example into Java code.
Step One: Import the asList static method.
The first step in this example requires that we import the Arrays.asList static method and the java.util.List class as well.
We import the java.util.Random class for the purposes of creating some random data to work with.
Step Three: Invoke the asList method.
Invoke the java.util.Arrays asList method and assign the result returned to List of type java.lang.Integer.
Step Four: Inspect the result.
In the fourth step we inspect the result returned by printing some of the information to console out.
We are specifically interested in the type of result returned in step three and we should also confirm that the List instance contains some random Integer values.
Step Five: Run the example.
In the fifth step we run the example by clicking on the play button indicated by the red pointer.
Step Six: Review the output.
Review the output and note that the result is an instance of Arrays$ArrayList — this List is an immutable implementation of the List interface and if we try to modify this list an java.lang.UnsupportedOperationException runtime exception will be thrown.
Step Seven: Attempt to add an Integer to the result List.
In the seventh step we attempt to add an Integer to the result List and when we run the Groovy example script an instance of UnsupportedOperationException will be thrown because the result List is an instance of Arrays$ArrayList, which is immutable.
If we need to change the contents of the resultant List then we’ll need to convert the result into an instance of java.util.ArrayList, which is mutable.
This concludes the walk-through regarding the Java 8 Arrays asList method — the tutorial conclusion follows.
Java java.util.Arrays asList Performance
The asList method in the java.util.Arrays returns an instance of java.util.Arrays$ArrayList (Arrays.ArrayList) which is read-only and is also backed directly by the array passed to the asList method.
The Arrays$ArrayList class differs from the java.util.ArrayList class in that it is read only and since mutator operations are unsupported invoking any of these methods will result an an UnsupportedOperationException being thrown.
Given that the Arrays.ArrayList class is read only, it will be unsuitable for use in situations we need to change the contents.
Converting from a Java array to an Arrays.ArrayList involves a single step and hence it performs very well, boasting an O(1) efficiency.
I’ve included a direct link to the Java 11 project source code for the Arrays$ArrayList static inner class on GitHub on the right-hand side of this page.
About This Guide
This instructional is a content experiment based on research from the StackOverflow post entitled How to convert int[] into List<Integer> in Java? along with some observations discovered while doing keyword research using SEMrush.
Other options are available as well — refer to the parent article regarding how to turn an array into an ArrayList along with the other guides listed in the see also section below.
See Also
- Tutorial: Learn how to convert an array to an ArrayList in Java!
- This instructional demonstrates how to use the Java ArrayList and stream method to transform an array of primitives efficiently.
- Learn how to convert a Java array into an ArrayList via the ArrayList constructor here!
- Learn how to use the Guava Lists.newArrayList method in this guide!
- A tutorial regarding how to transform an array to an ArrayList stream Java can be found here.
- Adding elements to a List in Java is easy in this introductory article geared toward beginner programmers!
- Introduction to the Java Programming Language
- Read about the discovery of a performance problem due to autoboxing in Java here.











