Java9新特性系列(便利的集合工厂方法)

Java8前时代

在Java8版本以前,创建一个只读不可变的集合,先要初始化,然后塞数据,然后置为只读:

1
2
3
4
5
Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
set = Collections.unmodifiableSet(set);

上面的方式占用太多行,能不能用单行表达式呢?用如下方式:

1
2
3
4
Set<String> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("a", "b", "c")));
Set<String> set = Collections.unmodifiableSet(new HashSet<String>() {{
add("a"); add("b"); add("c");
}});

Java8

在Java8中可以用流的方法创建,具体可以看之前的一篇文章Java8新特性系列(Stream),实现方法如下:

1
Set<String> set = Collections.unmodifiableSet(Stream.of("a", "b", "c").collect(toSet()));

Java9

Java9中引入了很多方便的API,Convenience Factory Methods for Collections,即集合工厂方法,官方Feature,上述的实现中Java9中有如下实现方式:

1
2
3
Set<String> set = Collections.unmodifiableSet(new HashSet<String>() {{
add("a"); add("b"); add("c");
}});

也可以用如下方式:

1
Set<String> set = Set.of("a", "b", "c");

Java9中List提供了一系列类似的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Returns an immutable list containing zero elements.
* @since 9
*/
static <E> List<E> of() {
return ImmutableCollections.List0.instance();
}

/**
* Returns an immutable list containing one element.
* @since 9
*/
static <E> List<E> of(E e1) {
return new ImmutableCollections.List1<>(e1);
}

/**
* Returns an immutable list containing one element.
* @since 9
*/
static <E> List<E> of(E e1) {
return new ImmutableCollections.List1<>(e1);
}

/**
* Returns an immutable list containing three elements.
* @since 9
*/
static <E> List<E> of(E e1, E e2, E e3) {
return new ImmutableCollections.ListN<>(e1, e2, e3);
}

...

/**
* Returns an immutable list containing ten elements.
* @since 9
*/
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) {
return new ImmutableCollections.ListN<>(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);
}

@SafeVarargs
@SuppressWarnings("varargs")
static <E> List<E> of(E... elements) {
switch (elements.length) { // implicit null check of elements
case 0:
return ImmutableCollections.List0.instance();
case 1:
return new ImmutableCollections.List1<>(elements[0]);
case 2:
return new ImmutableCollections.List2<>(elements[0], elements[1]);
default:
return new ImmutableCollections.ListN<>(elements);
}
}

Java9中Set、Map都有类似的方法,创建只读不可变的集合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Set.of()
...
Map.of()
Map.of(k1, v1)
Map.of(k1, v1, k2, v2)
Map.of(k1, v1, k2, v2, k3, v3)
...
Map.ofEntries(Map.Entry<K,V>...)
Map.Entry<K,V> entry(K k, V v)
Map.ofEntries(
entry(k1, v1),
entry(k2, v2),
entry(k3, v3),
// ...
entry(kn, vn)
);