8.8.5 Java9新增的不可变集合
8.8.5 Java9新增的不可变集合
Java9
终于增加这个功能了,以前假如要创建一个包含6个元素的set
集合,程序需要先创建Set
集合,然后调用6次add()
方法向Set
集合中添加元素。
使用of方法创建不可变集合
Java9
对此进行了简化,程序直接调用Set
、List
、Map
的of()
方法即可创建包含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 | import java.util.*; |
1 | [Go, Kotlin, Java, Swift] |
小结
创建不可变Set
、不可变List
比较简单,程序只要为它们的of
方法传入N个集合元素即可创建Set
、List
集合。
创建不可变的Map
集合有两个方法:
- 使用
of
方法时只要依次传入多个key-Value
对即可; - 还可使用
ofEntries()
方法,该方法可接受多个Enty
对象,因此程序显式使用Map.entry
方法来创建MapEntry
对象。使用这种方法比较美观,建议使用ofEntries
方法。
Map接口的of方法不支持变参
还有就是Map
接口的of
方法不支持变参,所以Map
接口的of
方法只能创建最多包含10个key-value
的map
。
如果超过10个的话需要使用ofEntries
方法