Collections.unmodifiableSet(Set) in Java

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

Syntax:

<String> Set<E> java.util.Collections.unmodifiableSet(Set<? extends E> s)

This method takes one argument. This method returns an unmodifiable view of the specified set.

Parameters: One parameter is required for this method.

s: the set for which an unmodifiable view is to be returned.

Returns: an unmodifiable view of the specified set.

Exceptions: NA

Approach

Java

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

public class CollectionsunmodifiableSet {
    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.unmodifiableSet(set));

    }
}

Output:

[Java, C++, Hello]


No comments:

Post a Comment