Java Collections unmodifiableSortedSet() Method
Description
The Java Collections unmodifiableSortedSet() method is used to return a immutable (thread-safe) sorted set backed by the specified sorted set.
Declaration
Following is the declaration for java.util.Collections.unmodifiableSortedSet() method.
public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s)
Parameters
s − This is the sorted set to be "wrapped" in a immutable sorted set.
Return Value
The method call returns a immutable view of the specified sorted set.
Exception
NA
Getting Immutable Set From a SortedSet of Integer Example
The following example shows the usage of Java Collection unmodifiableSortedSet(SortedSet) method. We've created a SortedSet object of Integer. Few entries are added and then using unmodifiableSortedSet(SortedSet) method, we've retrieved the immutable version of sorted set and printed the sorted set.
package com.tutorialspoint;
import java.util.Collections;
import java.util.TreeSet;
import java.util.SortedSet;
public class CollectionsDemo {
public static void main(String[] args) {
// create sorted set
SortedSet<Integer> sortedSet = new TreeSet<Integer>();
// populate the sorted set
sortedSet.add(1);
sortedSet.add(2);
sortedSet.add(3);
// create a immutable sorted set
SortedSet<Integer> immutableSet = Collections.unmodifiableSortedSet(sortedSet);
System.out.println("Immutable sorted set is :"+immutableSet);
}
}
Output
Let us compile and run the above program, this will produce the following result.
Immutable sorted set is :[1, 2, 3]
Getting Immutable Set From a SortedSet of String Example
The following example shows the usage of Java Collection unmodifiableSortedSet(SortedSet) method. We've created a SortedSet object of String and String. Few entries are added and then using unmodifiableSortedSet(SortedSet) method, we've retrieved the immutable version of map and printed the map.
package com.tutorialspoint;
import java.util.Collections;
import java.util.TreeSet;
import java.util.SortedSet;
public class CollectionsDemo {
public static void main(String[] args) {
// create sorted set
SortedSet<String> sortedSet = new TreeSet<String>();
// populate the sorted set
sortedSet.add("TP");
sortedSet.add("IS");
sortedSet.add("BEST");
// create a immutable sorted set
SortedSet<String> immutableSet = Collections.unmodifiableSortedSet(sortedSet);
System.out.println("Immutable sorted set is :"+immutableSet);
}
}
Output
Let us compile and run the above program, this will produce the following result.
Immutable sorted set is :[BEST, IS, TP]
Getting Immutable Set From a SortedSet of Object Example
The following example shows the usage of Java Collection unmodifiableSortedSet(SortedSet) method. We've created a SortedSet object of String and Student object. Few entries are added and then using unmodifiableSortedSet(SortedSet) method, we've retrieved the immutable version of map and printed the map.
package com.tutorialspoint;
import java.util.Collections;
import java.util.TreeSet;
import java.util.SortedSet;
public class CollectionsDemo {
public static void main(String[] args) {
// create sorted set
SortedSet<Student> sortedSet = new TreeSet<Student>();
// populate the sorted set
sortedSet.add(new Student(1, "Julie"));
sortedSet.add(new Student(2, "Robert"));
sortedSet.add(new Student(3, "Adam"));
// create a immutable sorted set
SortedSet<Student> immutableSet = Collections.unmodifiableSortedSet(sortedSet);
System.out.println("Immutable sorted set is :"+immutableSet);
}
}
class Student implements Comparable<Student> {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
@Override
public boolean equals(Object obj) {
Student s = (Student)obj;
return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
}
@Override
public int compareTo(Student student) {
return this.rollNo - student.rollNo;
}
}
Output
Let us compile and run the above program, this will produce the following result.
Immutable sorted set is :[[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]