Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Sometimes in Java, we need to create a small list or convert an array into a list for convenience. Java provides some helper methods for this.

In this tutorial, we’ll compare the two main ways of initializing small ad-hoc arrays: List.of() and Array.asList().

2. Using Arrays.asList()

Arrays.asList(), introduced in Java 1.2, simplifies the creation of a List object, which is a part of the Java Collections Framework. It can take an array as input and create the List object of the provided array:

Integer[] array = new Integer[]{1, 2, 3, 4};
List<Integer> list = Arrays.asList(array);
assertThat(list).containsExactly(1,2,3,4);

As we can see, it is very easy to create a simple List of Integers.

2.1. Unsupported Operations on the Returned List

The method asList() returns a fixed-size list. Therefore, adding and removing new elements throws an UnsupportedOperationException:

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
assertThrows(UnsupportedOperationException.class, () -> list.add(6));

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
assertThrows(UnsupportedOperationException.class, () -> list.remove(1));

2.2. Working With Arrays

We should note that the list doesn’t create a copy of the input array. Instead, it wraps the original array with the List interface. Therefore, changes to the array reflect on the list too:

Integer[] array = new Integer[]{1,2,3};
List<Integer> list = Arrays.asList(array);
array[0] = 1000;
assertThat(list.get(0)).isEqualTo(1000);

2.3. Changing the Returned List

Additionally, the list returned by Arrays.asList() is mutable. That is, we can change the individual elements of the list:

List<Integer> list = Arrays.asList(1, 2, 3, 4);
list.set(1, 1000);
assertThat(list.get(1)).isEqualTo(1000);

Eventually, this can lead to undesired side effects causing bugs that are difficult to find. When an array is provided as input, the change on the list will also be reflected on the array:

Integer[] array = new Integer[]{1, 2, 3};
List<Integer> list = Arrays.asList(array);
list.set(0,1000);
assertThat(array[0]).isEqualTo(1000);

Let’s see another way to create lists.

3. Using List.of()

In contrast to Arrays.asList(), Java 9 introduced a more convenient method, List.of(). This creates instances of unmodifiable List objects:

String[] array = new String[]{"one", "two", "three"};
List<String> list = List.of(array);
assertThat(list).containsExactly("two", "two", "three");

3.1. Differences From Arrays.asList()

The main difference from Arrays.asList() is that List.of() returns an immutable list that is a copy of the provided input array. For this reason, changes to the original array aren’t reflected on the returned list:

String[] array = new String[]{"one", "two", "three"};
List<String> list = List.of(array);
array[0] = "thousand";
assertThat(list.get(0)).isEqualTo("one");

Additionally, we cannot modify the elements of the list. If we try to, it will throw UnsupportedOperationException:

List<String> list = List.of("one", "two", "three");
assertThrows(UnsupportedOperationException.class, () -> list.set(1, "four"));

3.2. Null Values

We should also note that List.of() doesn’t allow null values as input and will throw a NullPointerException:

assertThrows(NullPointerException.class, () -> List.of("one", null, "two"));

4. Conclusion

This short article explores the creation of Lists in Java using List.of() and Arrays.asList().

As always, the full examples code is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.