Collections synchronizedList() method in Java with Examples
The synchronizedList() method of java.util.Collections class is used to return a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list.
Syntax:
public static <T> List<T> synchronizedList(List<T> list)
Parameters: This method takes the list as a parameter to be “wrapped” in a synchronized list.
Return Value: This method returns a synchronized view of the specified list.
Below are the examples to illustrate the synchronizedList() method
Example 1:
// Java program to demonstrate // synchronizedList() 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> list = new ArrayList<String>(); // populate the list list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); // printing the Collection System.out.println("List : " + list); // create a synchronized list List<String> synlist = Collections .synchronizedList(list); // printing the Collection System.out.println("Synchronized list is : " + synlist); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } |
List : [A, B, C, D, E] Synchronized list is : [A, B, C, D, E]
Example 2:
// Java program to demonstrate // synchronizedList() method for Integer Value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of List<Integer> List<Integer> list = new ArrayList<Integer>(); // populate the list list.add(20); list.add(30); list.add(40); list.add(50); list.add(60); // printing the Collection System.out.println("List : " + list); // create a synchronized list List<Integer> synlist = Collections .synchronizedList(list); // printing the Collection System.out.println("Synchronized list is : " + synlist); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } |
List : [20, 30, 40, 50, 60] Synchronized list is : [20, 30, 40, 50, 60]
Recommended Posts:
- Collections max() method in Java with Examples
- Collections min() method in Java with Examples
- Collections replaceAll() method in Java with Examples
- Collections swap() method in Java with Examples
- Collections synchronizedCollection() method in Java with Examples
- Collections copy() method in Java with Examples
- Collections newSetFromMap() method in Java with Examples
- Collections fill() method in Java with Examples
- Collections indexOfSubList() method in Java with Examples
- Collections list() method in Java with Examples
- Collections synchronizedMap() method in Java with Examples
- Collections checkedMap() method in Java with Examples
- Collections synchronizedSet() method in Java with Examples
- Collections synchronizedSortedMap() method in Java with Examples
- Collections synchronizedSortedSet() method in Java with Examples
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.



