Java Collections 类的 unmodifiableNavigableSet() 方法用于获取指定可导航集的不可修改视图。
用法
以下是 unmodifiableNavigableSet() 方法的声明:
public static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s)
参数
参数 | 描述 | 必需/可选 |
---|---|---|
s | 它是将返回不可修改视图的可导航集。 | Required |
返回
unmodifiableNavigableSet() 方法返回指定可导航集的不可修改视图。
异常
NA
兼容版本
Java 1.8 及以上
例子1
import java.util.*;
public class CollectionsUnmodifiableNavigableSetExample1 {
public static void main(String[] args) {
NavigableSet<Integer> set = new TreeSet<>();
Collections.addAll(set, 1,2,3,4,5);
System.out.println("Initial Set:" + set);
NavigableSet<Integer> set2 = Collections.unmodifiableNavigableSet(set);
System.out.println("Unmodifiable Navigable Set:" + set2);
//Modifying the original set
set.add(15);
System.out.println("Unmodifiable Navigable Set:" + set2);
}
}
输出:
Initial Set:[1, 2, 3, 4, 5] Unmodifiable Navigable Set:[1, 2, 3, 4, 5] Unmodifiable Navigable Set:[1, 2, 3, 4, 5, 15]
例子2
import java.util.*;
public class CollectionsUnmodifiableNavigableSetExample2 {
public static void main(String[] args) {
NavigableSet<String> set = new TreeSet<>();
Collections.addAll(set, "Google", "Facebook", "Instagram");
System.out.println("Initial Set:" + set);
NavigableSet<String> set2 = Collections.unmodifiableNavigableSet(set);
System.out.println("Unmodifiable Navigable Set:" + set2);
//Modifying the original set
set.add("Twitter");
System.out.println("Unmodifiable Navigable Set:" + set2);
}
}
输出:
Initial Set:[Facebook, Google, Instagram] Unmodifiable Navigable Set:[Facebook, Google, Instagram] Unmodifiable Navigable Set:[Facebook, Google, Instagram, Twitter]
例子3
import java.util.*;
public class CollectionsUnmodifiableNavigableSetExample3 {
public static void main(String[] args) {
NavigableSet<Integer> set = new TreeSet<>();
Collections.addAll(set, 11, 13, 17);
System.out.println("Original Set:" + set);
NavigableSet<Integer> set2 = Collections.unmodifiableNavigableSet(set);
set2.add(19);
}
}
输出:
Original Set:[11, 13, 17] Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.Collections$UnmodifiableCollection.add(Collections.java:1056) at myPackage.CollectionsUnmodifiableNavigableSetExample3.main(CollectionsUnmodifiableNavigableSetExample3.java:9)
相关用法
- Java Collections unmodifiableNavigableMap()用法及代码示例
- Java Collections unmodifiableSet()用法及代码示例
- Java Collections unmodifiableList()用法及代码示例
- Java Collections unmodifiableSortedMap()用法及代码示例
- Java Collections unmodifiableMap()用法及代码示例
- Java Collections unmodifiableCollection()用法及代码示例
- Java Collections unmodifiableSortedSet()用法及代码示例
- Java Collections synchronizedSortedSet()用法及代码示例
- Java Collections checkedQueue()用法及代码示例
- Java Collections checkedSet()用法及代码示例
- Java Collections copy()用法及代码示例
- Java Collections checkedMap()用法及代码示例
- Java Collections synchronizedNavigableSet()用法及代码示例
- Java Collections singleton()用法及代码示例
- Java Collections fill()用法及代码示例
- Java Collections nCopies()用法及代码示例
- Java Collections emptySet()用法及代码示例
- Java Collections newSetFromMap()用法及代码示例
- Java Collections checkedSortedMap()用法及代码示例
- Java Collections addAll()用法及代码示例
注:本文由纯净天空筛选整理自 Java Collections unmodifiableNavigableSet() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。