Collections.synchronizedSet(Set) in Java

Collections.synchronizedSet(Set): This method is available in java.util.Collections class of Java.

Syntax:

<E> Set<E> java.util.Collections.synchronizedSet(Set<E> s)

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

Parameters: One parameter is required for this method.

s: the set to be "wrapped" in a synchronized set.

Returns: a synchronized view of the specified set.

Exceptions: NA

Approach

Java

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

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

        Set<String> set = new HashSet<String>();

        set.add("Hello");
        set.add("Java");
        set.add("C++");

        System.out.println(Collections.synchronizedSet(set));

    }
}

Output:

[Java, C++, Hello]


No comments:

Post a Comment