📜  Java9 Factory Methods

📅  最后修改于: 2020-10-14 00:42:35             🧑  作者: Mango

Java 9工厂方法

Java 9 Collection库包括用于List,Set和Map接口的静态工厂方法。这些方法对于创建少量集合很有用。

假设,如果要创建一个包含5个元素的列表,则需要编写以下代码。

Java列表示例

import java.util.ArrayList;
import java.util.List;
public class FactoryMethodsExample {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("Java");
list.add("JavaFX");
list.add("Spring");
list.add("Hibernate");
list.add("JSP");
for(String l : list){
System.out.println(l);
}
}
}

输出:

Java
JavaFX
Spring
Hibernate
JSP

在上面的代码中,为每个列表元素重复调用add方法,而在Java 9中,我们可以使用工厂方法在一行代码中执行add方法。

工厂收集方法

工厂方法是静态方法的一种特殊类型,用于创建集合的不可修改实例。这意味着我们可以使用这些方法来创建少量元素的列表,集合和映射。

它是不可修改的,因此添加新元素将引发java.lang.UnsupportedOperationException

每个接口都有其自己的工厂方法,下表中列出了所有方法。

列表界面的工厂方法

Modifiers Methods Description
static List Of() It It returns an immutable list containing zero elements.
static List of(E e1) It It returns an immutable list containing one element.
static List of(E… elements) It It returns an immutable list containing an arbitrary number of elements.
static List of(E e1, E e2) It It returns an immutable list containing two elements.
static List of(E e1, E e2, E e3) It It returns an immutable list containing three elements.
static List of(E e1, E e2, E e3, E e4) It It returns an immutable list containing four elements.
static List of(E e1, E e2, E e3, E e4, E e5) It It returns an immutable list containing five elements.
static List of(E e1, E e2, E e3, E e4, E e5, E e6) It It returns an immutable list containing six elements.
static List of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) It It returns an immutable list containing seven elements.
static List of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) It It returns an immutable list containing eight elements.
static List of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) It It returns an immutable list containing nine elements.
static List of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) It It returns an immutable list containing ten elements.

Java 9列表工厂方法示例

在Java 9中,我们可以借助List.of()工厂方法以各种简单的方式编写此代码。

import java.util.List;
public class FactoryMethodsExample {
public static void main(String[] args) {
List list = List.of("Java","JavaFX","Spring","Hibernate","JSP");
for(String l:list) {
System.out.println(l);
}
}
}

输出:

Java
JavaFX
Spring
Hibernate
JSP

Java 9 Set接口

Java Set接口提供了一个Set.of()静态工厂方法,该方法用于创建不可变的集合。用此方法创建的set实例具有以下特征。

  • 这是一成不变的
  • 没有空元素
  • 如果所有元素都是可序列化的,则可以序列化。
  • 没有重复的元素。
  • set元素的迭代顺序未指定,可能会发生变化。

Java 9 Set接口工厂方法

下表包含Set接口的工厂方法。

Modifier and Type Method Description
static Set of() It It returns an immutable set containing zero elements.
static Set of(E e1) It It returns an immutable set containing one element.
static Set of(E… elements) It It returns an immutable set containing an arbitrary number of elements.
static Set of(E e1, E e2) It It returns an immutable set containing two elements.
static Set of(E e1, E e2, E e3) It It returns an immutable set containing three elements.
static Set of(E e1, E e2, E e3, E e4) It It returns an immutable set containing four elements.
static Set of(E e1, E e2, E e3, E e4, E e5) It It returns an immutable set containing five elements.
static Set It It returns an immutable set containing six elements.
static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) It It returns an immutable set containing seven elements.
static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) It It returns an immutable set containing eight elements.
static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) It It returns an immutable set containing nine elements.
static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) It It returns an immutable set containing ten elements.

Java 9 Set接口工厂方法示例

import java.util.Set;
public class FactoryMethodsExample {
public static void main(String[] args) {
Set set = Set.of("Java","JavaFX","Spring","Hibernate","JSP");
for(String l:set) {
System.out.println(l);
}
}
}

输出:

Spring
JavaFX
JSP
Java
Hibernate

Java 9 Map Interface工厂方法

在Java 9中,Map包含Map.of()和Map.ofEntries()静态工厂方法,这些方法提供了创建不可变地图的便捷方法。

通过这些方法创建的地图具有以下特征。

  • 这是一成不变的
  • 它不允许空键和值
  • 如果所有键和值都可序列化,则可序列化
  • 它在创建时拒绝重复的密钥
  • 映射的迭代顺序不确定,可能会发生变化。

Java 9 Map Interface工厂方法

下表包含Map接口的工厂方法。

Modifier and Type Method Description
static Map of() It returns an immutable map containing zero mappings.
static Map of(K k1, V v1) It returns an immutable map containing a single mapping.
static Map of(K k1, V v1, K k2, V v2) It returns an immutable map containing two mappings.
static Map of(K k1, V v1, K k2, V v2, K k3, V v3) It returns an immutable map containing three mappings.
static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) It returns an immutable map containing four mappings.
static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) It returns an immutable map containing five mappings.
static Map 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) It returns an immutable map containing six mappings.
static Map 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) It returns an immutable map containing seven mappings.
static Map 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) It returns an immutable map containing eight mappings.
static Map 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) It returns an immutable map containing nine mappings.
static Map 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) It returns an immutable map containing ten mappings.
static Map ofEntries(Map.Entry… entries) It returns an immutable map containing keys and values extracted from the given entries.

Java 9 Map Interface Factory方法示例

import java.util.Map;
public class FactoryMethodsExample {
public static void main(String[] args) {
Map map = Map.of(101,"JavaFX",102,"Hibernate",103,"Spring MVC");
for(Map.Entry m : map.entrySet()){  
System.out.println(m.getKey()+" "+m.getValue());
}
}
}

输出:

102 Hibernate
103 Spring MVC
101 JavaFX

Java 9 Map Interface ofEntries()方法示例

在Java 9中,除了静态Map.of()方法之外,Map接口还包含一个静态方法Map.ofEntries()。此方法用于创建Map.Entry实例的地图。

在以下示例中,我们将在多个map.entry实例的帮助下创建地图实例。

import java.util.Map;
public class FactoryMethodsExample {
public static void main(String[] args) {
// Creating Map Entry
Map.Entry e1 = Map.entry(101, "Java");
Map.Entry e2 = Map.entry(102, "Spring");
// Creating Map using map entries
Map map = Map.ofEntries(e1,e2);
// Iterating Map 
for(Map.Entry m : map.entrySet()){  
System.out.println(m.getKey()+" "+m.getValue());
}
}
}

输出:

102 Spring
101 Java