原创 

Java 9 List, Set, Map等集合类新增工厂方法

分类:java    499人阅读    IT小君  2020-09-12 21:11

一. 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);
    
  }
}





点击广告,支持我们为你提供更好的服务

html5图标下拉搜索框自动匹配代码

html5 svg夜空中星星流星动画场景特效

HTML5 Canvas竖直流动线条背景动画特效

HTML5数字产品服务公司网站模板

js+css3抽奖转盘旋转点餐代码

html5 canvas进度条圆环图表统计动画特效

响应式时尚单品在线商城网站模板

canvas炫酷鼠标移动文字粒子特效

网页设计开发公司网站模板

css+js实现的颜色渐变数字时钟动画特效

html5 canvas彩色碎片组合球形旋转动画特效

小众时尚单品在线电子商务网站模板

HTML5现代家居装潢公司网站模板

响应式咖啡饮品宣传网站模板

css鼠标跟随文字模糊特效

响应式太阳能能源公司网站模板

jQuery右端悬浮带返回顶部特效

中小型创意设计服务公司网站模板

有机水果蔬菜HTML5网站模板

现代时尚家具公司网站模板

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务