? List接口,set接口,map接口:增加了一个静态方法of,可以给集合一次性添加多个元素
? static
? 当前集合存储的元素个数已经确定,不可改变
public class Demo01JDK9 {
public static void main(String[] args) {
List<String> list = List.of("a","b","a","c","d");
System.out.println(list);
Set<String> set = Set.of("a","b","c","d");
System.out.println(set);
}
}
需要注意以下两点:
1:of()方法只是Map,List,Set这三个接口的静态方法,其父类接口和子类实现并没有这类方法,比如 HashSet,ArrayList等待;
2:返回的集合是不可变的;
原文:https://www.cnblogs.com/anke-z/p/12585013.html