 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Synchronized Set from HashSet in Java
The synchronizedSet() method returns a synchronized (thread-safe) sorted set backed by the specified sorted set.
First, create a HashSet and add elements −
Set<Integer> hs = new HashSet<Integer>(); hs.add(29); hs.add(879); hs.add(88); hs.add(788); hs.add(456);
Now, to get synchronized set, use the synchronizedSet() method −
Set s = Collections.synchronizedSet(hs);
The following is an example to get synchronized set from HashSet −
Example
import java.util.*; public class Demo { public static void main(String args[]) { Set<Integer> hs = new HashSet<Integer>(); hs.add(29); hs.add(879); hs.add(88); hs.add(788); hs.add(456); Set s = Collections.synchronizedSet(hs); System.out.println("Synchronized Set = "+s); } }  Output
Synchronized Set = [788, 88, 456, 29, 879]
Advertisements
 