Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ba92ca7

Browse files
committed
add
1 parent 5125760 commit ba92ca7

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎ch17/02_Collection_Factories.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
3+
4+
## 收集工厂
5+
6+
`Collections` 类提供了创建某些包含零个或多个对同一对象的引用的集合的简便方法。 最简单的这种集合是空的:
7+
8+
```java
9+
<T> List<T> emptyList() // 返回空列表(不可变)
10+
<K,V> Map<K,V> emptyMap() // 返回空映射(不可变)
11+
<T> Set<T> emptySet() // 返回空集(不可变的)
12+
```
13+
14+
空集合在实现返回值集合的方法时很有用,它们可以用来表示没有值返回。 每个方法都返回一个对 `Collection` 的单例内部类的实例的引用。 因为这些实例是不可变的,所以它们可以安全地共享,所以调用其中一个工厂方法不会导致创建对象。 在泛型之前,`Collections` 字段 `EMPTY_SET`,`EMPTY_LIST``EMPTY_MAP` 通常用于 `Java` 中的相同目的,但现在用处不大,因为它们的原始类型在使用时会生成未经检查的警告。
15+
16+
`Collections` 类还为您提供了创建仅包含单个成员的集合对象的方法:
17+
18+
```java
19+
<T> Set<T> singleton(T o) // 返回只包含指定对象的不可变集合
20+
<T> List<T> singletonList(T o) // 返回只包含指定对象的不可变列表
21+
<K,V> Map<K,V> singletonMap(K key, V value) // 返回一个不可变的映射,只将键 K 映射到值 V.
22+
```
23+
24+
同样,这些可以用于为接受值集合的方法提供单个输入值。
25+
26+
最后,可以创建一个包含给定对象的许多副本的列表。
27+
28+
```java
29+
<T> List<T> nCopies(int n, T o) // 返回一个不可变列表,其中包含对对象 o 的 n 个引用
30+
```
31+
32+
由于 `nCopies` 生成的列表是不可变的,它只需要包含一个物理元素来提供所需长度的列表视图。这样的列表通常被用作构建进一步集合的基础 - 例如,作为构造函数或 `addAll` 方法的参数。
33+
34+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /