The Java Collections synchronizedSortedSet() method is used to return a synchronized (thread-safe) sorted set backed by the specified sorted set.
DeclarationFollowing 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 ValueThe method call returns a synchronized view of the specified sorted set.
NA
Getting Synchronized SortedSet From a Unsynchronized SortedSet of Integer ExampleThe 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 ]]
java_util_collections.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4