VOOZH about

URL: https://www.geeksforgeeks.org/java/collections-synchronizedcollection-method-in-java-with-examples/

⇱ Collections synchronizedCollection() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Collections synchronizedCollection() method in Java with Examples

Last Updated : 8 Jul, 2021

The synchronizedCollection() method of java.util.Collections class is used to return a synchronized (thread-safe) collection backed by the specified collection. In order to guarantee serial access, it is critical that all access to the backing collection is accomplished through the returned collection.
Syntax: 
 

public static <T> Collection<T>
 synchronizedCollection(Collection<T> c)


Parameters: This method takes the collection c as a parameter to be "wrapped" in a synchronized collection.
Return Value: This method returns a synchronized view of the specified collection.
Below are the examples to illustrate the synchronizedCollection() method
Example 1: 
 


Output
Collection : [A, B, C, D, E]
Synchronized view of collection : [A, B, C, D, E]

Example 2: 
 


Output: 
Collection : [20, 30, 40, 50, 60]
Synchronized view is : [20, 30, 40, 50, 60]

 
Comment