The Java Collections synchronizedList(List<T>) method is used to return a synchronized (thread-safe) list backed by the specified list.
DeclarationFollowing is the declaration for java.util.Collections.synchronizedList() method.
public static <T> List<T> synchronizedList(List<T> list)Parameters
list − The list to be "wrapped" in a synchronized list.
Return ValueThe method call returns a synchronized view of the specified list.
NA
Getting Synchronized List From a Unsynchronized List of Integer ExampleThe following example shows the usage of Java Collection synchronizedList(List) method. We've created a List object with some integers. Using synchronizedList(List) method, we've retrieved the synchronized version of list and printed the list.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5)); // synchronized version of list List<Integer> c = Collections.synchronizedList(list); System.out.println("Synchronized list: "+ c); } }Output
Let us compile and run the above program, this will produce the following result −
Synchronized list: [1, 2, 3, 4, 5]Getting Synchronized List From a Unsynchronized List of String Example
The following example shows the usage of Java Collection synchronizedList(List) method. We've created a List object with some strings. Using synchronizedList(List) method, we've retrieved the synchronized version of list and printed the list.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("Welcome","to","Tutorialspoint")); // synchronized version of list List<String> c = Collections.synchronizedList(list); System.out.println("Synchronized list: "+ c); } }Output
Let us compile and run the above program, this will produce the following result −
Synchronized list: [Welcome, to, Tutorialspoint]Getting Synchronized List From a Unsynchronized List of Object Example
The following example shows the usage of Java Collection synchronizedList(List) method. We've created a List object with some Student objects. Using synchronizedList(List) method, we've retrieved the synchronized version of list and printed the list.
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"), new Student(2, "Robert"), new Student(3, "Adam"))); // synchronized version of list List<Student> c = Collections.synchronizedList(list); System.out.println("Synchronized list: "+ c); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }Output
Let us compile and run the above program, this will produce the following result −
Synchronized list: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_collections.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4