Arrays.asList vs new ArrayList(Arrays.asList())

1. Overview

In this short tutorial, we’ll take a look at the differences between Arrays.asList(array) and ArrayList(Arrays.asList(array)).

2. Arrays.asList

Let’s start with the Arrays.asList method.

Using this method, we can convert from an array to a fixed-size List objectThis List is just a wrapper that makes the array available as a list. No data is copied or created.

Also, we can’t modify its length because adding or removing elements is not allowed.

However, we can modify single items inside the array. Note that all the modifications we make to the single items of the List will be reflected in our original array:

String[] stringArray = new String[] { "A", "B", "C", "D" };
List stringList = Arrays.asList(stringArray);

Now, let’s see what happens if we modify the first element of stringList:

stringList.set(0, "E");
 
assertThat(stringList).containsExactly("E", "B", "C", "D");
assertThat(stringArray).containsExactly("E", "B", "C", "D");

As we can see, our original array was modified, too. Both the list and the array now contain exactly the same elements in the same order.

Let’s now try to insert a new element to stringList:

stringList.add("F");
java.lang.UnsupportedOperationException
	at java.base/java.util.AbstractList.add(AbstractList.java:153)
	at java.base/java.util.AbstractList.add(AbstractList.java:111)

As we can see, adding/removing elements to/from the List will throw java.lang.UnsupportedOperationException.

3. ArrayList(Arrays.asList(array))

Similar to the Arrays.asList method, we can use ArrayList<>(Arrays.asList(array)) when we need to create a List out of an array.

But, unlike our previous example, this is an independent copy of the array, which means that modifying the new list won’t affect the original array. Additionally, we have all the capabilities of a regular ArrayList, like adding and removing elements:

String[] stringArray = new String[] { "A", "B", "C", "D" }; 
List stringList = new ArrayList<>(Arrays.asList(stringArray));

Now let’s modify the first element of stringList:

stringList.set(0, "E");
 
assertThat(stringList).containsExactly("E", "B", "C", "D");

And now, let’s see what happened with our original array:

assertThat(stringArray).containsExactly("A", "B", "C", "D");

As we can see, our original array remains untouched.

Before wrapping up, if we take a look at the JDK source code, we can see the Arrays.asList method returns a type of ArrayList that is different from java.util.ArrayList. The main difference is that the returned ArrayList only wraps an existing array — it doesn’t implement the add and remove methods.

4. Conclusion

In this short article, we took a look at the differences between two ways of converting an array into an ArrayList. We saw how those two options behave and the difference between how they implement their internal arrays.

As always, the code samples can be found over on GitHub.

Related posts:

Java Program to Search for an Element in a Binary Search Tree
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Java Program to Implement Ternary Search Algorithm
Spring Boot: Customize the Jackson ObjectMapper
Java Program to Implement Merge Sort Algorithm on Linked List
@DynamicUpdate with Spring Data JPA
Abstract class và Interface trong Java
Java Program to Represent Linear Equations in Matrix Form
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Spring Boot - Google OAuth2 Sign-In
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Java Program to Implement SynchronosQueue API
Sử dụng CyclicBarrier trong Java
Java Program to Implement Quick Sort with Given Complexity Constraint
Sử dụng CountDownLatch trong Java
ETL with Spring Cloud Data Flow
Java Program to find the number of occurrences of a given number using Binary Search approach
A Guide to the ResourceBundle
Spring’s RequestBody and ResponseBody Annotations
Spring Data – CrudRepository save() Method
Java Program to Implement Gale Shapley Algorithm
Java Program to implement Associate Array
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Construct an Expression Tree for an Postfix Expression
Guide to WeakHashMap in Java
Java Program to Represent Graph Using 2D Arrays
Documenting a Spring REST API Using OpenAPI 3.0
Giới thiệu HATEOAS
Java Program to Perform Left Rotation on a Binary Search Tree
Read an Outlook MSG file
Java Program to Perform the Sorting Using Counting Sort
Spring Boot - Build Systems