Collections unmodifiableSet() method in Java with Examples Last Updated : 08 Oct, 2018 Comments Improve Suggest changes Like Article Like Report The unmodifiableSet() method of java.util.Collections class is used to return an unmodifiable view of the specified set. This method allows modules to provide users with "read-only" access to internal sets. Query operations on the returned set "read through" to the specified set, and attempts to modify the returned set, whether direct or via its iterator, result in an UnsupportedOperationException. The returned set will be serializable if the specified set is serializable. Syntax: public static <T> Set<T> unmodifiableSet(Set<? extends T> s) Parameters: This method takes the set as a parameter for which an unmodifiable view is to be returned. Return Value: This method returns an unmodifiable view of the specified set. Below are the examples to illustrate the unmodifiableSet() method Example 1: Java // 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); } } } Output: unmodifiable Set: [X, Y] Example 2: For UnsupportedOperationException Java // 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); } } } Output: unmodifiable Set: [X, Y] Trying to modify the unmodifiable set Exception thrown : java.lang.UnsupportedOperationException Comment More infoAdvertise with us Next Article Collections unmodifiableSet() method in Java with Examples R rohitprasad3 Follow Improve Article Tags : Advance Java Java-Collections Java - util package Java-Functions Practice Tags : Java-Collections Similar Reads Collections unmodifiableList() method in Java with Examples The unmodifiableList() method of java.util.Collections class is used to return an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists. Query operations on the returned list "read through" to the specified list, and attempts t 2 min read Collections unmodifiableMap() method in Java with Examples The unmodifiableMap() method of java.util.Collections class is used to return an unmodifiable view of the specified map. This method allows modules to provide users with "read-only" access to internal maps. Query operations on the returned map "read through" to the specified map, and attempts to mod 2 min read Collections unmodifiableSortedMap() method in Java with Examples The unmodifiableSortedMap() method of java.util.Collections class is used to return an unmodifiable view of the specified sorted map. This method allows modules to provide users with "read-only" access to internal sorted maps. Query operations on the returned sorted map "read through" to the specifi 2 min read Collections unmodifiableCollection() method in Java with Examples The unmodifiableCollection() method of java.util.Collections class is used to return an unmodifiable view of the specified collection. This method allows modules to provide users with "read-only" access to internal collections. Query operations on the returned collection "read through" to the specif 2 min read Java Collections unmodifiableSortedSetâ() Method with Examples The unmodifiableSortedSet() method of Java Collections is available in TreeSet. A Tree Set is a data structure that can store elements in order. Syntax: SortedSet<datatype> data = new TreeSet<String>(); where, datatype specifies the type of elementsdata is the input data. unmodifiableSor 2 min read Like