- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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]
- Related Articles
- Get Synchronized List from ArrayList in java
- Get size of HashSet in Java
- Get Enumeration over HashSet in Java
- Set vs HashSet vs TreeSet in Java
- Internal working of Set/HashSet in Java
- Get Sub Set from TreeSet in Java
- Get Tail Set from TreeSet in Java
- Get Head Set from Java TreeSet
- Remove specified element from HashSet in Java
- synchronized Keyword in Java
- Get navigable key set in java from NavigableMap
- Remove all elements from a HashSet in Java
- Remove single element from a HashSet in Java
- HashSet in Java
- Java Program to get Sub Set from TreeSet

Advertisements