- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Collections synchronizedSortedSet() Method
Description
The Java Collections synchronizedSortedSet() method is used to return a synchronized (thread-safe) sorted set backed by the specified sorted set.
Declaration
Following is the declaration for java.util.Collections.synchronizedSortedSet() method.
public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s)
Parameters
s − This is the sorted set to be "wrapped" in a synchronized sorted set.
Return Value
The method call returns a synchronized view of the specified sorted set.
Exception
NA
Getting Synchronized SortedSet From a Unsynchronized SortedSet of Integer Example
The following example shows the usage of Java Collection synchronizedSortedSet(SortedSet) method. We've created a SortedSet object of Integer. Few entries are added and then using synchronizedSortedSet(SortedSet) method, we've retrieved the synchronized 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 synchronized sorted set
SortedSet<Integer> synset = Collections.synchronizedSortedSet(sortedSet);
System.out.println("Synchronized sorted set is :"+synset);
}
}
Output
Let us compile and run the above program, this will produce the following result.
Synchronized sorted set is :[1, 2, 3]
Getting Synchronized SortedSet From a Unsynchronized SortedSet of String Example
The following example shows the usage of Java Collection synchronizedSortedSet(SortedSet) method. We've created a SortedSet object of String and String. Few entries are added and then using synchronizedSortedSet(SortedSet) method, we've retrieved the synchronized 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 synchronized sorted set
SortedSet<String> synset = Collections.synchronizedSortedSet(sortedSet);
System.out.println("Synchronized sorted set is :"+synset);
}
}
Output
Let us compile and run the above program, this will produce the following result.
Synchronized sorted set is :[BEST, IS, TP]
Getting Synchronized SortedSet From a Unsynchronized SortedSet of Object Example
The following example shows the usage of Java Collection synchronizedSortedSet(SortedSet) method. We've created a SortedSet object of String and Student object. Few entries are added and then using synchronizedSortedSet(SortedSet) method, we've retrieved the synchronized 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 synchronized sorted set
SortedSet<Student> synset = Collections.synchronizedSortedSet(sortedSet);
System.out.println("Synchronized sorted set is :"+synset);
}
}
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.
Synchronized sorted set is :[[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]