📅  最后修改于: 2020-10-14 00:42:35             🧑  作者: Mango
Java 9 Collection库包括用于List,Set和Map接口的静态工厂方法。这些方法对于创建少量集合很有用。
假设,如果要创建一个包含5个元素的列表,则需要编写以下代码。
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 |
Of() | It It returns an immutable list containing zero elements. |
static |
of(E e1) | It It returns an immutable list containing one element. |
static |
of(E… elements) | It It returns an immutable list containing an arbitrary number of elements. |
static |
of(E e1, E e2) | It It returns an immutable list containing two elements. |
static |
of(E e1, E e2, E e3) | It It returns an immutable list containing three elements. |
static |
of(E e1, E e2, E e3, E e4) | It It returns an immutable list containing four elements. |
static |
of(E e1, E e2, E e3, E e4, E e5) | It It returns an immutable list containing five elements. |
static |
of(E e1, E e2, E e3, E e4, E e5, E e6) | It It returns an immutable list containing six elements. |
static |
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 |
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 |
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 |
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中,我们可以借助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 Set接口提供了一个Set.of()静态工厂方法,该方法用于创建不可变的集合。用此方法创建的set实例具有以下特征。
下表包含Set接口的工厂方法。
Modifier and Type | Method | Description |
---|---|---|
static |
of() | It It returns an immutable set containing zero elements. |
static |
of(E e1) | It It returns an immutable set containing one element. |
static |
of(E… elements) | It It returns an immutable set containing an arbitrary number of elements. |
static |
of(E e1, E e2) | It It returns an immutable set containing two elements. |
static |
of(E e1, E e2, E e3) | It It returns an immutable set containing three elements. |
static |
of(E e1, E e2, E e3, E e4) | It It returns an immutable set containing four elements. |
static |
of(E e1, E e2, E e3, E e4, E e5) | It It returns an immutable set containing five elements. |
static |
It It returns an immutable set containing six elements. | |
static |
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 |
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 |
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 |
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. |
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包含Map.of()和Map.ofEntries()静态工厂方法,这些方法提供了创建不可变地图的便捷方法。
通过这些方法创建的地图具有以下特征。
下表包含Map接口的工厂方法。
Modifier and Type | Method | Description |
---|---|---|
static |
of() | It returns an immutable map containing zero mappings. |
static |
of(K k1, V v1) | It returns an immutable map containing a single mapping. |
static |
of(K k1, V v1, K k2, V v2) | It returns an immutable map containing two mappings. |
static |
of(K k1, V v1, K k2, V v2, K k3, V v3) | It returns an immutable map containing three mappings. |
static |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
ofEntries(Map.Entry extends K,? extends V>… entries) | It returns an immutable map containing keys and values extracted from the given entries. |
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.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