原文: https://howtodoinjava.com/java9/create-immutable-collections-factory-methods/

学习使用新的创建不可变集合,例如不可变列表不可变集不可变映射 Java 9 中的工厂方法。

  1. Table of Contents
  2. Create Immutable List
  3. Create Immutable Set
  4. Create Immutable Map

创建不可变列表

使用List.of()静态工厂方法创建不可变列表。 它具有以下不同的重载版本:

  1. static <E> List<E> of()
  2. static <E> List<E> of(E e1)
  3. static <E> List<E> of(E e1, E e2)
  4. static <E> List<E> of(E e1, E e2, E e3)
  5. static <E> List<E> of(E e1, E e2, E e3, E e4)
  6. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5)
  7. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6)
  8. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
  9. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
  10. static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
  11. 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)
  12. //varargs
  13. static <E> List<E> of(E... elements)

这些方法创建的List实例具有以下特征:

  1. 这些列表是不可变的。 不能在这些列表中添加,删除或替换元素。 调用任何可变方法(即addaddAllclearremoveremoveAllreplaceAll)将始终导致抛出UnsupportedOperationException
  2. 它们不允许null元素。 尝试添加null元素将导致NullPointerException
  3. 如果所有元素都是可序列化的,它们可以可序列化的
  4. 列表中元素的顺序与提供的参数或提供的数组中的元素的顺序相同。

让我们看一些使用不可变列表的例子。

  1. package com.howtodoinjava;
  2. import java.util.List;
  3. public class ImmutableCollections
  4. {
  5. public static void main(String[] args)
  6. {
  7. List<String> names = List.of("Lokesh", "Amit", "John");
  8. //Preserve the elements order
  9. System.out.println(names);
  10. //names.add("Brian"); //UnsupportedOperationException occured
  11. //java.lang.NullPointerException
  12. //List<String> names2 = List.of("Lokesh", "Amit", "John", null);
  13. }
  14. }
  15. Output:
  16. [Lokesh, Amit, John]

创建不可变集

Set的行为与List非常相似,只是差异很小。 例如:

  1. Set也不允许重复元素。 传递的任何重复元素将导致IllegalArgumentException
  2. 集合元素的迭代顺序未指定,可能会发生变化。

所有Set工厂方法都具有与List相同的签名。

  1. static <E> Set<E> of()
  2. static <E> Set<E> of(E e1)
  3. static <E> Set<E> of(E e1, E e2)
  4. static <E> Set<E> of(E e1, E e2, E e3)
  5. static <E> Set<E> of(E e1, E e2, E e3, E e4)
  6. static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5)
  7. static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6)
  8. static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
  9. static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
  10. static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
  11. 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)
  12. //varargs
  13. static <E> Set<E> of(E... elements)

我们来看几个不可变集的例子。

  1. import java.util.Set;
  2. public class ImmutableCollections {
  3. public static void main(String[] args) {
  4. Set<String> names = Set.of("Lokesh", "Amit", "John");
  5. //Elements order not fixed
  6. System.out.println(names);
  7. //names.add("Brian"); //UnsupportedOperationException occured
  8. //java.lang.NullPointerException
  9. //Set<String> names2 = Set.of("Lokesh", "Amit", "John", null);
  10. //java.lang.IllegalArgumentException
  11. //Set<String> names3 = Set.of("Lokesh", "Amit", "John", "Amit");
  12. }
  13. }

创建不可变映射

Map工厂方法与ListSet重载工厂方法相同。 唯一的区别是方法的签名采用交替的键和值作为参数。 例如:

  1. static <K,V> Map<K,V> of()
  2. static <K,V> Map<K,V> of(K k1, V v1)
  3. static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2)
  4. ...
  5. ...
  6. static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)

Java 9 还提供了一种特殊的方法来创建 Map 条目实例。

  1. static <K,V> Map.Entry<K,V> entry(K k, V v)

让我们举一个在 Java 9 中创建不可变Map的示例。

  1. import java.util.Map;
  2. public class ImmutableCollections {
  3. public static void main(String[] args) {
  4. Map<String, String> names = Map.ofEntries(
  5. Map.entry("1", "Lokesh"),
  6. Map.entry("2", "Amit"),
  7. Map.entry("3", "Brian"));
  8. System.out.println(names);
  9. //UnsupportedOperationException
  10. //names.put("2", "Ravi");
  11. }
  12. }
  13. Output:
  14. {1=Lokesh, 2=Amit, 3=Brian}

显然,在 Java 9 中创建不可变集合的新工厂方法具有很高的可读性和易用性

请在评论部分中将您对这些不可变集合的观点放下。

学习愉快!