1. Overview
1.概述
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.
在Java中,有时我们需要创建一个小的列表或将一个数组转换为一个列表,以方便使用。Java为此提供了一些辅助方法。
In this tutorial, we’ll compare the two main ways of initializing small ad-hoc arrays: List.of() and Array.asList().
在本教程中,我们将比较初始化小型特设数组的两种主要方式。List.of()和Array.asList()。
2. Using Arrays.asList()
2.使用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:
Arrays.asList(),在Java 1.2中引入,简化了List对象的创建,它是JavaCollections Framework的一部分。它可以接受一个数组作为输入,并创建所提供数组的List对象。
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.
我们可以看到,创建一个简单的List的Integers非常容易。
2.1. Unsupported Operations on the Returned List
2.1.对返回列表的不支持的操作
The method asList() returns a fixed-size list. Therefore, adding and removing new elements throws an UnsupportedOperationException:
方法asList()返回一个固定大小的列表。因此,添加和删除新元素会抛出一个不支持操作的异常。
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
2.2.使用数组工作
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:
我们应该注意,列表并没有创建一个输入数组的副本。相反,它用 List 接口包装了原始数组。因此,对数组的改变也反映在列表上。
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
2.3.改变返回的列表
Additionally, the list returned by Arrays.asList() is mutable. That is, we can change the individual elements of the list:
此外,由Arrays.asList()返回的列表是可变的。也就是说,我们可以改变列表中的各个元素。
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()
3.使用List.of()
In contrast to Arrays.asList(), Java 9 introduced a more convenient method, List.of(). This creates instances of unmodifiable List objects:
与Arrays.asList()相比,Java 9引入了一个更方便的方法,List.of()。这将创建不可修改的List对象的实例。
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()
3.1.与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:
与Arrays.asList()的主要区别在于,List.of()返回一个不可变的列表,它是所提供的输入数组的副本。由于这个原因,原始数组的变化不会反映在返回的列表中。
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
3.2 空值
We should also note that List.of() doesn’t allow null values as input and will throw a NullPointerException:
我们还应该注意,List.of()不允许null值作为输入,并且会抛出NullPointerException。
assertThrows(NullPointerException.class, () -> List.of("one", null, "two"));
4. Conclusion
4.总结
This short article explores the creation of Lists in Java using List.of() and Arrays.asList().
这篇短文探讨了在Java中使用List.of()和Arrays.asList()创建Lists。
As always, the full examples code is available over on GitHub.
一如既往,完整的示例代码可在GitHub上获得over。