8.8.5 Java9新增的不可变集合

8.8.5 Java9新增的不可变集合

Java9终于增加这个功能了,以前假如要创建一个包含6个元素的set集合,程序需要先创建Set集合,然后调用6次add()方法向Set集合中添加元素。

使用of方法创建不可变集合

Java9对此进行了简化,程序直接调用SetListMapof()方法即可创建包含N个元素的不可变集合,这样一行代码就可创建包含N个元素的集合。
注意of方法创建的是不可变集合,不可变意味着程序不能向集合中添加元素,也不能从集合中删除元素。

Set接口的of方法

方法 描述
static <E> Set<E> of() Returns an unmodifiable set containing zero elements.
static <E> Set<E> of(E e1) Returns an unmodifiable set containing one element.
static <E> Set<E> of(E... elements) Returns an unmodifiable set containing an arbitrary number of elements.
static <E> Set<E> of(E e1, E e2) Returns an unmodifiable set containing two elements.
static <E> Set<E> of(E e1, E e2, E e3) Returns an unmodifiable set containing three elements.
static <E> Set<E> of(E e1, E e2, E e3, E e4) Returns an unmodifiable set containing four elements.
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5) Returns an unmodifiable set containing five elements.
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6) Returns an unmodifiable set containing six elements.
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) Returns an unmodifiable set containing seven elements.
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) Returns an unmodifiable set containing eight elements.
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) Returns an unmodifiable set containing nine elements.
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) Returns an unmodifiable set containing ten elements.

List接口的of方法

方法 描述
static <E> List<E> of() Returns an unmodifiable list containing zero elements.
static <E> List<E> of(E e1) Returns an unmodifiable list containing one element.
static <E> List<E> of(E... elements) Returns an unmodifiable list containing an arbitrary number of elements.
static <E> List<E> of(E e1, E e2) Returns an unmodifiable list containing two elements.
static <E> List<E> of(E e1, E e2, E e3) Returns an unmodifiable list containing three elements.
static <E> List<E> of(E e1, E e2, E e3, E e4) Returns an unmodifiable list containing four elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5) Returns an unmodifiable list containing five elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6) Returns an unmodifiable list containing six elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) Returns an unmodifiable list containing seven elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) Returns an unmodifiable list containing eight elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) Returns an unmodifiable list containing nine elements.
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) Returns an unmodifiable list containing ten elements.

Map接口的of方法

方法 描述
static <K,​V> Map<K,​V> of() Returns an unmodifiable map containing zero mappings.
static <K,​V> Map<K,​V> of(K k1, V v1) Returns an unmodifiable map containing a single mapping.
static <K,​V> Map<K,​V> of(K k1, V v1, K k2, V v2) Returns an unmodifiable map containing two mappings.
static <K,​V> Map<K,​V> of(K k1, V v1, K k2, V v2, K k3, V v3) Returns an unmodifiable map containing three mappings.
static <K,​V> Map<K,​V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) Returns an unmodifiable map containing four mappings.
static <K,​V> Map<K,​V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) Returns an unmodifiable map containing five mappings.
static <K,​V> Map<K,​V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) Returns an unmodifiable map containing six mappings.
static <K,​V> Map<K,​V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) Returns an unmodifiable map containing seven mappings.
static <K,​V> Map<K,​V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8) Returns an unmodifiable map containing eight mappings.
static <K,​V> Map<K,​V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) Returns an unmodifiable map containing nine mappings.
static <K,​V> Map<K,​V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) Returns an unmodifiable map containing ten mappings.

使用Map接口的ofEntries方法创建不可变Map

方法 描述
static <K,​V> Map<K,​V> ofEntries(Map.Entry<? extends K,​? extends V> ... entries) Returns an unmodifiable map containing keys and values extracted from the given entries.

程序 使用of方法创建不可变集合

下程序示范了如何创建不可变集合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;

public class Java9Collection {
public static void main(String[] args) {
// 创建包含4个元素的Set集合
Set set = Set.of("Java", "Kotlin", "Go", "Swift");
System.out.println(set);
// 不可变集合,下面代码导致运行时错误
// set.add("Ruby");
// 创建包含4个元素的List集合
List list = List.of(34, -25, 67, 231);
System.out.println(list);
// 不可变集合,下面代码导致运行时错误
// list.remove(1);
// 创建包含3组key-value对的Map集合
Map map = Map.of("语文", 89, "数学", 82, "英语", 92);
System.out.println(map);
// 不可变集合,下面代码导致运行时错误
// map.remove("语文");
// 使用Map.entry()方法显式构建key-value对
Map map2 = Map.ofEntries(Map.entry("语文", 89), Map.entry("数学", 82), Map.entry("英语", 92));
System.out.println(map2);
}
}
1
2
3
4
[Go, Kotlin, Java, Swift]
[34, -25, 67, 231]
{数学=82, 英语=92, 语文=89}
{数学=82, 英语=92, 语文=89}

小结

创建不可变Set、不可变List比较简单,程序只要为它们的of方法传入N个集合元素即可创建SetList集合。

创建不可变的Map集合有两个方法:

  • 使用of方法时只要依次传入多个key-Value对即可;
  • 还可使用ofEntries()方法,该方法可接受多个Enty对象,因此程序显式使用Map.entry方法来创建MapEntry对象。使用这种方法比较美观,建议使用ofEntries方法。

Map接口的of方法不支持变参

还有就是Map接口的of方法不支持变参,所以Map接口的of方法只能创建最多包含10个key-valuemap
如果超过10个的话需要使用ofEntries方法