java.util.Collections.synchronizedSortedSet() Method


Description

The synchronizedSortedSet() method is used to get 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 thethe 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

Example

The following example shows the usage of java.util.Collections.synchronizedSortedSet()

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String[] args) {
   
      // create set
      SortedSet<String> set = new TreeSet<String>();

      // populate the set
      set.add("TP");
      set.add("IS");
      set.add("FOR");
      set.add("TECHIES");      

      // create a synchronized sorted set
      SortedSet sorset = Collections.synchronizedSortedSet(set);

      System.out.println("Sorted set is :"+sorset);
   }
}

Let us compile and run the above program, this will produce the following result.

Sorted set is :[FOR, IS, TECHIES, TP]
java_util_collections.htm
Advertisements