Java Utility Library

Java Collections - synchronizedList() Method



The java.util.Collections.synchronizedList() method returns a synchronized (thread-safe) list backed by the specified list.

Syntax

public static <T> List<T> synchronizedList(List<T> list)

Here, T is the type of element in the list.


Parameters

list Specify the list to be "wrapped" in a synchronized list.

Return Value

Returns a synchronized view of the specified list.

Exception

NA.

Example:

In the example below, the java.util.Collections.synchronizedList() method returns a synchronized view of the given list.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a list object
    List<Integer> MyList = new ArrayList<Integer>();

    //populating the list
    MyList.add(10);
    MyList.add(20);
    MyList.add(30);
    MyList.add(40);

    //printing the list
    System.out.println("MyList contains: " + MyList); 

    //creating an synchronized view of the list
    List NewList = Collections.synchronizedList(MyList);

    //printing the synchronized list
    System.out.println("NewList contains: " + NewList); 
  }
}

The output of the above code will be:

MyList contains: [10, 20, 30, 40]
NewList contains: [10, 20, 30, 40]

❮ Java.util - Collections