java.util.Collections.synchronizedSet() Method

Advertisements


Description

The synchronizedSet() 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.synchronizedSet() method.

public static <T> Set<T> synchronizedSet(Set<T> s)

Parameters

  • s--This is the set to be "wrapped" in a synchronized set.

Return Value

  • The method call returns a synchronized view of the specified set.

Exception

  • NA

Example

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

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String[] args) {
      // create set
      Set<String> set = new HashSet<String>();
      
      // populate the set
      set.add("TP");
      set.add("IS");
      set.add("FOR");
      set.add("TECHIES");
      
      
      // create a synchronized set
      Set<String> synset = Collections.synchronizedSet(set);
     
      System.out.println("Synchronized set is :"+synset);
   }
}

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

Synchronized set is :[FOR, IS, TECHIES, TP]



Advertisements
Advertisements