Java 简明教程

Java - Collection Factory Methods

Factory Methods for Collection

在 Java 9 中, collections 已得到增强,可以采用少量新方法,轻松简洁地创建不可变列表。这些新*factory methods*已添加到 ListSetMap界面,以创建不可变实例。这些工厂方法主要是便利的工厂方法,目的是以更简洁且更简洁的方式创建集合。

In Java 9, collections were enhanced to have few new methods to create immutable list in an easy and concise way. These new factory methods were added to List, Set, and Map interfaces to create immutable instances. These factory methods are mainly convenience factory methods in order to create a collection in less verbose and in concise way.

Syntax

在 Java 9 之前,使用以下语法创建不可变列表。

Before Java 9, following syntax was used to create a immutable list.

List unmodifiableList = Collections.unmodifiableList(arrayList);

其中 arrayList 是一个可变列表实例。因此需要创建一个列表,然后使用 unmodifiableList() 我们将得到一个不可变的实例,我们无法从中添加/移除元素。

Where arrayList is a mutable list instance. So we are required to create a list and then using unmodifiableList() we get an immutable instance from which we cannot add/remove an element.

Factory Methods of List Interface

现在从 Java 9 开始,可以使用以下方法来创建一个不可变的列表。

Now from Java 9, following methods can be used to create a immutable list.

static <E> List<E> of();
static <E> List<E> of(E e1);
static <E> List<E> of(E... elements)
static <E> List<E> of(E e1, E e2);
static <E> List<E> of(E e1, E e2, E e3);
static <E> List<E> of(E e1, E e2, E e3, E e4);
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5);
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6);
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7);
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8);
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8,E e9);
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);

Syntax

因此,从 Java 9 开始,可以使用以下语法来创建一个不可变列表。of(E…​ 元素) 方法可用于让一个不可变列表有超过 10 个元素。

So from Java 9 onwards, following syntax can be used to create a immutable list. of(E…​ elements) method can be used to have more than 10 elements in an immutable list.

List<String> unmodifiableList = List.of("A", "B", "C");

Example of List Interface Factory Methods Before Java 9

在此处,我们正在创建 Java 9 之前的不可修改列表。

Here, we are creating unmodifiable list before Java 9.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Tester {
   public static void main(String[] args) {
      List<String> list = new ArrayList<>();

      list.add("A");
      list.add("B");
      list.add("C");
      list = Collections.unmodifiableList(list);
      System.out.println(list);
   }
}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

[A, B, C]

Example of List Interface Factory Methods in Java 9

在此处,我们正在 Java 9 中创建不可修改列表。

Here, we are creating unmodifiable list in Java 9.

package com.tutorialspoint;

import java.util.List;

public class Tester {
   public static void main(String[] args){
	   List<String> list =  List.of("A","B","C");
	   System.out.println(list);
   }
}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

[A, B, C]

Factory Methods of Set Interface

按照类似方式,Set 接口具有这些新方法,用于创建一个不可修改的 Set 以获取不能从中添加/移除元素的 set 的实例。

On similar pattern, Set interface is having these new methods to create a unmodifiable Set to get an instance of a set from which we cannot add/remove an element.

static <E> Set<E> of();
static <E> Set<E> of(E e1);
static <E> Set<E> of(E... elements)
static <E> Set<E> of(E e1, E e2);
static <E> Set<E> of(E e1, E e2, E e3);
static <E> Set<E> of(E e1, E e2, E e3, E e4);
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5);
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6);
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7);
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8);
static <E> Set<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8,E e9);
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);

Syntax

因此,从 Java 9 开始,可以使用以下语法来创建一个不可变的 set。of(E…​ 元素) 方法可用于让一个不可变的 set 有超过 10 个元素。

So from Java 9 onwards, following syntax can be used to create a immutable set. of(E…​ elements) method can be used to have more than 10 elements in an immutable set.

Set<String> unmodifiableSet = Set.of("A", "B", "C");

Example of Set Interface Factory Methods Before Java 9

此处,我们在 Java 9 之前创建一个不可修改的 set。

Here, we are creating unmodifiable set before Java 9.

package com.tutorialspoint;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Tester {
   public static void main(String[] args){
      Set<String> set = new HashSet<>();
      set.add("A");
      set.add("B");
      set.add("C");
      set = Collections.unmodifiableSet(set);
      System.out.println(set);
   }
}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

[A, B, C]

Example of Set Interface Factory Methods in Java 9

此处,我们在 Java 9 中创建一个不可修改的 set。

Here, we are creating unmodifiable set in Java 9.

package com.tutorialspoint;

import java.util.Set;

public class Tester {
   public static void main(String[] args){
	   Set<String> set =  Set.of("A","B","C");
	   System.out.println(set);
   }
}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

[A, B, C]

Factory Methods of Map Interface

对于 Map 接口,我们已经 ofEntries(…​) 可用于接受除 of() 方法之外的参数变参,如下所示。

In case of Map interface, we’ve ofEntries(…​) can be used to accept var args parameter other than of() methods as shown below.

static <K,V> Map<K,V> of();
static <K,V> Map<K,V> of(K k1, V v1);
static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2);
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3);
static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4);
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);
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);
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);
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);
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);
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 k1, V v10);

因此,从 Java 9 开始,可以使用以下语法来创建一个不可变的 map。

So from Java 9 onwards, following syntax can be used to create a immutable map.

Map<String, String> unmodifiableMap = Map.of("A","Apple", "B", "Boy", "C", "Cat");

Example of Map Interface Factory Methods Before Java 9

此处,我们在 Java 9 之前创建一个不可修改的 Map。

Here, we are creating unmodifiable Map before Java 9.

package com.tutorialspoint;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Tester {
   public static void main(String[] args){
      Map<String, String> map = new HashMap<>();

      map.put("A","Apple");
      map.put("B","Boy");
      map.put("C","Cat");
      map = Collections.unmodifiableMap(map);
      System.out.println(map);
   }
}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

{A=Apple, B=Boy, C=Cat}

Example of Map Interface Factory Methods in Java 9

此处,我们在 Java 9 中创建一个不可修改的 Map。

Here, we are creating unmodifiable Map in Java 9.

package com.tutorialspoint;

import java.util.Map;

public class Tester {
   public static void main(String[] args){
      Map<String, String> map = Map.of("A","Apple","B","Boy","C","Cat");
      System.out.println(map);
   }
}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

{C=Cat, A=Apple, B=Boy}