Collections.synchronizedCollection(Collection) in Java

Collections.synchronizedCollection(Collection): This method is available in java.util.Collections class of Java.

Syntax:

<E> Collection<E> java.util.Collections.synchronizedCollection(Collection<E> c)

This method takes one argument. This method returns a synchronized (thread-safe) collection backed by the specified collection.

Parameters: One parameter is required for this method.

c: the collection to be "wrapped" in a synchronized collection.

Returns: a synchronized view of the specified collection.

Exceptions: NA

Approach

Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CollectionssynchronizedCollection {
    public static void main(String[] args) {

        List<Integer> arrlist = new ArrayList<Integer>();

        arrlist.add(12);
        arrlist.add(56);
        arrlist.add(899);
        arrlist.add(65);
        arrlist.add(5);

        System.out.println(Collections.synchronizedCollection(arrlist));

    }
}

Output:

[12, 56, 899, 65, 5]


No comments:

Post a Comment