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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。