O método unmodifiableSet() da classe java.util.Collections é usado para retornar uma visualização não modificável do conjunto especificado. Este método permite que os módulos forneçam aos usuários acesso “somente leitura” aos conjuntos internos. As operações de consulta no conjunto retornado “lêem” para o conjunto especificado e as tentativas de modificar o conjunto retornado, seja direto ou por meio de seu iterador, resultam em uma UnsupportedOperationException.

O conjunto retornado será serializável se o conjunto especificado for serializável.

Sintaxe:

public static <T> Set<T> unmodifiableSet(Set<? extends T> s)

Parâmetros: Este método toma o conjunto como um parâmetro para o qual uma visão não modificável deve ser retornada.

Valor de retorno: Este método retorna uma visão não modificável do conjunto especificado.

Abaixo estão os exemplos para ilustrar o método unmodifiableSet()

Exemplo 1:

// Java program to demonstrate
// unmodifiableSet() method
// for <Character> value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
  
        try {
  
            // creating object of HashSet<Character>
            Set<Character> set = new HashSet<Character>();
  
            // populate the table
            set.add('X');
            set.add('Y');
  
            // make the set unmodifiable
            Set<Character>
                immutableSet = Collections
                                   .unmodifiableSet(set);
  
            // printing unmodifiableSet
            System.out.println("unmodifiable Set: "
                               + immutableSet);
        }
  
        catch (UnsupportedOperationException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
Saída:
Conjunto não modificável: [X, Y]

Exemplo 2: Para UnsupportedOperationException

// Java program to demonstrate
// unmodifiableSet() method
// for <Character> value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // creating object of HashSet<Character>
            Set<Character> set = new HashSet<Character>();
  
            // populate the table
            set.add('X');
            set.add('Y');
  
            // make the set unmodifiable
            Set<Character>
                immutableSet = Collections
                                   .unmodifiableSet(set);
  
            // printing unmodifiableSet
            System.out.println("unmodifiable Set: "
                               + immutableSet);
  
            System.out.println("\nTrying to modify"
                               + " the unmodifiable set");
            immutableSet.add('Z');
        }
  
        catch (UnsupportedOperationException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
Saída:
Conjunto não modificável: [X, Y]

Tentando modificar o conjunto não modificável
Exceção lançada: java.lang.UnsupportedOperationException