Java Collections emptyNavigableSet()
Java集合类的
emptyNavigableSet()方法用于获取本质上不可变的空可导航Set。
语法
以下是
emptyNavigableSet()方法的声明:
public static <E> NavigableSet<E> emptyNavigableSet()
参数
此方法不接受任何参数。
返回
emptyNavigableSet()方法将返回一个不包含元素的不可变导航Set。
异常
NA
兼容版本
Java 1.8及更高版本
示例1
import java.util.*;
public class CollectionsEmptyNavigableSetExample1 {
public static void main(String[] args) {
//Create an empty Set
NavigableSet<String> EmpNavSet = Collections.<String>emptyNavigableSet();
System.out.println("Empty Navigable Set: "+EmpNavSet);
}
}
输出:
示例2
import java.util.*;
public class CollectionsEmptyNavigableSetExample2 {
public static void main(String[] args) {
//Create an empty Navigable Set
NavigableSet<String> EmpNavSet = Collections.emptyNavigableSet();
System.out.println("Created empty immutable Navigable Set: "+EmpNavSet);
//try to add elements
EmpNavSet.add("A");
EmpNavSet.add("B");
}
}
输出:
Created empty immutable Navigable Set: []
Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1056)
at myPackage.CollectionsEmptyNavigableSetExample2.main(CollectionsEmptyNavigableSetExample2.java:9)
示例3
import java.util.*;
public class CollectionsEmptyNavigableSetExample3 {
public static void main(String[] args) {
//Create an empty Navigable Set
Set<Integer> empNavSet = Collections.emptyNavigableSet();
empNavSet.add(1);
empNavSet.add(2);
System.out.println("Created empty immutable Navigable Set: "+empNavSet);
}
}
输出:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1056)
at myPackage.CollectionsEmptyNavigableSetExample3.main(CollectionsEmptyNavigableSetExample3.java:7)