×

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]

Related Topics

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

Java Calculate Average of List

The list is a linear data structure used in Java to store ordered data collections. Additionally, it accepts duplicate values while maintaining insertion order. It is sometimes necessary to find...

3 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.

Java LinkedHashMap

LinkedHashMap implements the Map interface. It inherits the HashMap class. Some essential features of LinkedHashMap are: It contains the values based on the keys. It maintains the order of insertion. It contains unique...

5 minutes read.

Sort Elements by Frequency in Java

To sort the elements in Java by using frequency, we need an input array. We should create a function that sorts the elements in an array by using their frequencies...

3 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10...

3 minutes read.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 minutes read.

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.

Java Command Line Arguments

Java command line arguments Command line argument is an input or argument to the program when you run the program. In Java, we can take the inputs from the console, and...

2 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.