Getting Synchronized Set from Java HashSet
The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance to the support set is achieved through the brought put-off.
Syntax:
public static <T> Set<T>
synchronizedSet(Set<T> s)
Parameters:
This technique accepts the set as a boundary to be "wrapped" in a synchronized set.
Return Value:
This technique returns a synchronized perspective on the predefined set.
Example:
// simple Java program to demonstrate synchronizedSet() method in java
// for String Value
import java.io.*; //here, we are importing the java.io package into our program
import java.util.*; //here, we are importing the java.util package into our program
public class Hs {
public static void main(String[] argv) throws Exception
{
//here, we are declaring the try block if we get any exception it will throw it
try {
//here, we are creating the object of Set<String>
Set<String> s = new HashSet<String>();
//here, we are populating the set
s.add("10");
s.add("20");
s.add("30");
//here, we are printing the Collection
System.out.println("The Set after adding the elements are: " + s);
// here, we are creating a synchronized set
Set<String> syns = Collections.synchronizedSet(s);
// here, we are printing the set after performing the synchronized set
System.out.println("The Synchronized set is : "+ syns);
}
//here, we are declaring the catch block if we get any exception in the try //block then the catch block will catch it.
catch (IllegalArgumentException e) {
//if there are any exceptions then that exception will be printed here
System.out.println("Here, we are Exception thrown : " + e);
}
}
}
Output:
The Set after adding the elements are: [10, 20, 30]
The Synchronized set is: [10, 20, 30]
Example 1:
// simple Java program to demonstrate synchronizedSet() method in java
// for Integer Value
import java.io.*; //here, we are importing the java.io package into our program
import java.util.*; //here, we are importing the java.util package into our program
public class Hs {
public static void main(String[] argv) throws Exception
{
//here, we are declaring the try block if we get any exception it will throw it
try {
//here, we are creating the object of Set<String>
Set<Integer> s = new HashSet<Integer>();
//here, we are populating the set
s.add("55"); //here, we are adding the element 55 to the set
s.add("25"); //here, we are adding the element 25 to the set
s.add("35"); //here, we are adding the element 35 to the set
//here, we are printing the Collection
System.out.println("The Set after adding the elements are: " + s);
// here, we are creating a synchronized set
Set<String> syns = Collections.synchronizedSet(s);
// here, we are printing the set after performing the synchronized set
System.out.println("The Synchronized set is : "+ syns);
}
//here, we are declaring the catch block if we get any exception in the try //block then the catch block will catch it.
catch (IllegalArgumentException e) {
//if there are any exceptions then that exception will be printed here
System.out.println("Here, we are going to Exception the thrown : " + e);
}
}
}
Output:
The Set after adding the elements are: [55, 25, 35]
The Synchronized set is: [55, 25, 35]
Example 2:
// simple Java program to demonstrate synchronizedSet() method in java
// for Integer Value
import java.io.*; //here, we are importing the java.io package into our program
import java.util.*; //here, we are importing the java.util package into our program
public class Hs {
public static void main(String[] argv) throws Exception
{ //here, we are declaring the try block if we get any exception it will throw it
try {
//here, we are creating the object of Set<String>
Set<Integer> s = new HashSet<Integer>();
//here, we are populating the set
s.add(200);
s.add(400);
s.add(600);
s.add(800);
s.add(1000);
//here, we are printing the Collection or set of elements
System.out.println("The Set after adding the elements are: " + s);
// here, we are creating a synchronized set
Set<String> syns = Collections.synchronizedSet(s);
// here, we are printing the set after performing the synchronized set
System.out.println("The Synchronized set is : "+ syns);
}
//here, we are declaring the catch block if we get any exception in the try //block then the catch block will catch it.
catch (IllegalArgumentException e) {
//if there are any exceptions then that exception will be printed here
System.out.println("Here, we are going to Exception the thrown : " + e);
}
}
}
Output:
The Set after adding the elements are: [200, 400, 600, 800, 1000]
The Synchronized Set is: [200, 400, 600, 800, 1000]