Collections unmodifiableSet() method in Java with Examples
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 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); } } } |
unmodifiable Set: [X, Y]
Example 2: For 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); } } } |
unmodifiable Set: [X, Y] Trying to modify the unmodifiable set Exception thrown : java.lang.UnsupportedOperationException
Recommended Posts:
- Collections max() method in Java with Examples
- Collections min() method in Java with Examples
- Collections checkedList() method in Java with Examples
- Collections checkedCollection() method in Java with Examples
- Collections checkedSet() method in Java with Examples
- Collections checkedSortedMap() method in Java with Examples
- Collections fill() method in Java with Examples
- Collections copy() method in Java with Examples
- Collections enumeration() method in Java with Examples
- Collections checkedSortedSet() method in Java with Examples
- Collections unmodifiableList() method in Java with Examples
- Collections singletonMap() method in Java with Examples
- Collections asLifoQueue() method in Java with Examples
- Collections unmodifiableMap() method in Java with Examples
- Collections unmodifiableCollection() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



