一. List
创建一个List我们可以使用以下工厂方法:
// 创建空的列表
static <E> List<E> of()
// 创建包含一个、多个元素的列表
static <E> List<E> of(E... elements)
示例:
List<String> immutableList = List.of();
immutableList = List.of("one", "two", "three");
note:
1、List.of 元素不能包含null元素 不然会报 java.lang.NullPointerException
2、List.of生成的list是不可变的即不支持add,强行修改会报:java.lang.UnsupportedOperationException
将List.of生成list的转成可变及可包含空可进行如下转化:
List<String> mutableList = new ArrayList<String>(List.of("one", "two", "three"));
mutableList.add("four");
mutableList.add(null);
// 结果:
[one, two, three, four, null]
二. Set
创建set可以使用以下工厂方法:
// 创建空Set
static <E> Set<E> of()
// 创建包含一个或多个元素的Set
static <E> Set<E> of(E... elements)
示例:
Set<String> immutableSet = Set.of();
immutableSet = Set.of("one", "two", "three");
note:
同样Set.of 不能包含null和具有不可改变性质
转化如下:
Set<String> mutableSet = new HashSet<String>(Set.of("one", "two", "three"));
mutableSet.add("four");
mutableSet.add(null);
// 结果:
[null, four, one, two, three]
三、Map
创建Map可以使用以下工厂方法:
// 创建空的Map
static <K, V> Map<K, V> of()
// 创建包含一个元素的Map
static <K, V> Map<K, V> of(K k1, V v1)
// ...
// 创建包含十个元素的Map
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)
示例:
Map<Integer, String> immutableMap = Map.of();
immutableMap = Map.of(1, "one", 2, "two", 3, "three");
note:
同样Map.of 不能包含null和具有不可改变性质 转化如下:
Map<Integer, String> mutableMap = new HashMap<Integer, String>(Map.of(1, "one", 2, "two", 3, "three"));
mutableMap.put(4, "four");
mutableMap.put(5, null);
// 结果:
{1=one, 2=two, 3=three, 4=four, 5=null}
如果创建key多余10个,可以使用:Map.ofEntries(),其定义如下:
static <K, V> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries)
使用示例:
Map<Integer, String> newImmutableMap = Map.ofEntries(Map.entry(1, "one"), Map.entry(2, "two"), Map.entry(3, "three"));
以上示例源代码:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MainApp {
public static void main(String[] args) {
// List
System.out.println("--- List ---");
List<String> immutableList = List.of();
System.out.println(immutableList);
immutableList = List.of("one", "two", "three"/* , null */);
System.out.println(immutableList);
// immutableList.add("four");
List<String> mutableList = new ArrayList<String>(List.of("one", "two", "three"));
mutableList.add("four");
mutableList.add(null);
System.out.println(mutableList);
// Set
System.out.println("--- Set ---");
Set<String> immutableSet = Set.of();
System.out.println(immutableSet);
immutableSet = Set.of("one", "two", "three"/* , null */);
System.out.println(immutableSet);
// immutableSet.add("four");
Set<String> mutableSet = new HashSet<String>(Set.of("one", "two", "three"));
mutableSet.add("four");
mutableSet.add(null);
System.out.println(mutableSet);
// Map
System.out.println("--- Map ---");
Map<Integer, String> immutableMap = Map.of();
System.out.println(immutableMap);
immutableMap = Map.of(1, "one", 2, "two", 3, "three"/* , 4, null */);
System.out.println(immutableMap);
// immutableMap.put(4, "four");
Map<Integer, String> mutableMap = new HashMap<Integer, String>(Map.of(1, "one", 2, "two", 3, "three"));
System.out.println(mutableMap);
mutableMap.put(4, "four");
mutableMap.put(5, null);
System.out.println(mutableMap);
// Map with Entry
System.out.println("--- Map Entry ---");
Map<Integer, String> newImmutableMap = Map.ofEntries();
System.out.println(newImmutableMap);
newImmutableMap = Map.ofEntries(Map.entry(1, "one"), Map.entry(2, "two"), Map.entry(3, "three"));
System.out.println(newImmutableMap);
}
}