s-day04-单列和双列集合的of方法(了解)

This commit is contained in:
2026-08-03 11:58:20 +08:00
parent 1595d2a08e
commit 6d8d7ef388
+25
View File
@@ -0,0 +1,25 @@
package com.inmind.of04;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
在JDK9中对单列和双列集合,定义了一个静态方法of,它能快速创建出简单的集合,保证集合不变的
使用场景:
在业务编写时,可以设计为不能更改的固定值,营业中,打烊中,准备中
*/
public class OfDemo11 {
public static void main(String[] args) {
//List的of方法
List<Integer> list = List.of(1, 2, 3, 4, 5);
System.out.println(list);
//Set的of方法
Set<String> sets = Set.of("1", "2", "3", "4", "5");
System.out.println(sets);
//Map的of方法
Map<Integer, String> map = Map.of(1, "张三", 2, "lisi", 3, "wangwu");
System.out.println(map);
}
}