Collections.unmodifiableCollection(Collection) in Java

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

Syntax:

<E> Collection<E> java.util.Collections.unmodifiableCollection(Collection<? extends E> c)

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

Parameters: One parameter is required for this method.

c: the collection for which an unmodifiable view is to be returned.

Returns: an unmodifiable view of the specified collection.

Exceptions: NA

Approach

Java

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

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

    }
}

Output:

[Java, C++, Hello]


No comments:

Post a Comment