1. Overview
1.概述
This quick tutorial will show how to make an ArrayList immutable with the core JDK, with Guava and finally with Apache Commons Collections 4.
这个快速教程将展示如何用核心JDK、Guava和Apache Commons Collections 4制作一个ArrayList不可变。
This article is part of the “Java – Back to Basic” series here on Baeldung.
本文是“Java – Back to Basic“系列的一部分,在Baeldung这里。
2. With the JDK
2.使用 JDK
First, the JDK provides a nice way to get an unmodifiable collection out of an existing one:
首先,JDK提供了一种很好的方法来从现有的集合中得到一个不可修改的集合。
Collections.unmodifiableList(list);
The new collection should no longer be modifiable at this point:
这时,新的集合不应该再被修改。
@Test(expected = UnsupportedOperationException.class)
public void givenUsingTheJdk_whenUnmodifiableListIsCreated_thenNotModifiable() {
List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
List<String> unmodifiableList = Collections.unmodifiableList(list);
unmodifiableList.add("four");
}
2.1. With Java 9
2.1.有了Java 9
Since Java 9, we can use a List<E>.of(E… elements) static factory method to create an immutable list:
从Java 9开始,我们可以使用List<E>.of(E…elements)静态工厂方法来创建一个不可变的列表。
@Test(expected = UnsupportedOperationException.class)
public final void givenUsingTheJava9_whenUnmodifiableListIsCreated_thenNotModifiable() {
final List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
final List<String> unmodifiableList = List.of(list.toArray(new String[]{}));
unmodifiableList.add("four");
}
Notice how we have to convert the existing list into an array. This is because List.of(elements) accepts vararg parameters.
请注意,我们必须将现有的list转换成一个数组。这是因为List.of(elements)接受vararg参数。
3. With Guava
3.有番石榴
Guava provides similar functionality for creating its own version of ImmutableList:
Guava提供了类似的功能来创建自己的ImmutableList版本。
ImmutableList.copyOf(list);
Similarly – the resulting list should not be modifiable:
同样地–产生的列表不应该是可修改的。
@Test(expected = UnsupportedOperationException.class)
public void givenUsingGuava_whenUnmodifiableListIsCreated_thenNotModifiable() {
List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
List<String> unmodifiableList = ImmutableList.copyOf(list);
unmodifiableList.add("four");
}
Note that this operation will actually create a copy of the original list, not just a view.
注意,这个操作实际上会创建一个原始列表的副本,而不仅仅是一个视图。
Guava also provides a builder – this will return the strong-typed ImmutableList instead of simply List:
Guava还提供了一个构建器–它将返回强类型的ImmutableList而不是简单的List。
@Test(expected = UnsupportedOperationException.class)
public void givenUsingGuavaBuilder_whenUnmodifiableListIsCreated_thenNoLongerModifiable() {
List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
ImmutableList<String> unmodifiableList = ImmutableList.<String>builder().addAll(list).build();
unmodifiableList.add("four");
}
4. With the Apache Collections Commons
4.使用Apache Collections Commons
Finally, Commons Collection also provides an API to create an unmodifiable list:
最后,Commons Collection还提供了一个API来创建一个不可修改的列表。
ListUtils.unmodifiableList(list);
And again, modifying the resulting list should result in an UnsupportedOperationException:
再说一遍,修改产生的列表应该导致不支持操作的异常。
@Test(expected = UnsupportedOperationException.class)
public void givenUsingCommonsCollections_whenUnmodifiableListIsCreated_thenNotModifiable() {
List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
List<String> unmodifiableList = ListUtils.unmodifiableList(list);
unmodifiableList.add("four");
}
5. Conclusion
5.结论
This tutorial illustrates how to easily create an unmodifiable List out of an existing ArrayList using either the core JDK, Google Guava or Apache Commons Collections.
本教程说明了如何使用核心JDK、Google Guava或Apache Commons Collections轻松从现有的ArrayList创建一个不可修改的列表。
The implementation of all these examples and code snippets can be found over on Github – this is a Maven-based project, so it should be easy to import and run as it is.
所有这些例子和代码片段的实现都可以在Github上找到 – 这是一个基于Maven的项目,所以应该很容易导入并按原样运行。