进阶day04-单列和双列集合的of方法
This commit is contained in:
34
javaSE-day04/src/com/inmind/extend02/Demo10.java
Normal file
34
javaSE-day04/src/com/inmind/extend02/Demo10.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package com.inmind.extend02;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/*
|
||||||
|
在JDK9版本之后,对于单列和双列集合的接口中,定义一个静态方法 of方法,它能够快速的创建出一些
|
||||||
|
简单的集合,保证该集合数据不可变
|
||||||
|
|
||||||
|
注意:当我们要创建一些不可改变的基准数据时,就可以使用of方法
|
||||||
|
*/
|
||||||
|
public class Demo10 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//使用List接口的of方法,创建出不可改变的集合
|
||||||
|
ArrayList<Integer> lists = new ArrayList<>();
|
||||||
|
lists.add(1);
|
||||||
|
|
||||||
|
List<Integer> list = List.of(1, 2, 3, 4, 5, 5, 5);
|
||||||
|
System.out.println(list);
|
||||||
|
//以下对of方法得到的集合,进行增删改操作时,会保出java.lang.UnsupportedOperationException
|
||||||
|
// list.add(6);
|
||||||
|
// list.remove(5);
|
||||||
|
// list.set(0, 100);
|
||||||
|
|
||||||
|
Set<Integer> set = Set.of(1, 2, 3, 4);
|
||||||
|
System.out.println(set);
|
||||||
|
|
||||||
|
Map<String, String> map = Map.of("刘备", "孙尚香", "吕布", "貂蝉");
|
||||||
|
System.out.println(map);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user