java.util.Collections.unmodifiableSortedSet() 方法
描述
unmodifiableSortedSet() 方法用于获取指定排序集的不可修改视图。
声明
以下是 java.util.Collections.unmodifiableSortedSet() 方法的声明。
public static <T> SortedSet <T> unmodifiableSortedSet(SortedSet <T> s)
参数
s − 这是要为其返回不可修改视图的排序集。
返回值
方法调用返回指定排序集的不可修改视图。
异常
NA
示例
下面的例子展示了 java.util.Collections.unmodifiableSortedSet() 的用法。
package com.tutorialspoint;
import java.util.*;
public class CollectionsDemo {
public static void main(String[] s) {
// create sorted set
SortedSet<String> set = new TreeSet<String>();
// populate the set
set.add("Welcome");
set.add("to");
set.add("TP");
System.out.println("Initial set value: "+set);
// create unmodifiable sorted set
Set unmodsortset = Collections.unmodifiableSortedSet(set);
// try to modify the sorted set
unmodsortset.add("Hello");
}
}
让我们编译并运行上面的程序,这将产生以下结果.
Initial set value: [TP, Welcome, to] Exception in thread "main" java.lang.UnsupportedOperationException

