Java Collections synchronizedList() Method



Description

The Java Collections synchronizedList(List<T>) method is used to return a synchronized (thread-safe) list backed by the specified list.

Declaration

Following 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 Value

  • The method call returns a synchronized view of the specified list.

Exception

NA

Getting Synchronized List From a Unsynchronized List of Integer Example

The 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
Advertisements