×

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

Program to Find Square Root of a Number Without sqrt Method in Java

The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's...

3 minutes read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

JDK | Java Development Kit

Java Development Kit (JDK) The Java Development Kit is a software development environment used to create Java applications. The JDK includes JRE (Java Runtime Environment), an interpreter (java), a compiler (javac),...

3 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.