Collections synchronizedCollection() method in Java with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate synchronizedCollection()
// method for String Value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of List<String>
            List<String> vector = new ArrayList<String>();
  
            // populate the vector
            vector.add("A");
            vector.add("B");
            vector.add("C");
            vector.add("D");
            vector.add("E");
  
            // printing the Collection
            System.out.println("Collection : " + vector);
  
            // getting the syncronised view of Collection
            Collection<String> c = Collections
                                       .synchronizedCollection(vector);
  
            // printing the Collection
            System.out.println("Synchronized view"
                               + " of collecttion : " + c);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate synchronizedCollection()
// method for Integer Value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of List<String>
            List<Integer> vector = new ArrayList<Integer>();
  
            // populate the vector
            vector.add(20);
            vector.add(30);
            vector.add(40);
            vector.add(50);
            vector.add(60);
  
            // printing the Collection
            System.out.println("Collection : " + vector);
  
            // getting the syncronised view of Collection
            Collection<Integer> c = Collections
                                        .synchronizedCollection(vector);
  
            // printing the Collection
            System.out.println("Synchronized view is : " + c);
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.