Unmodifiable Set in Java
This post will discuss various methods to create an unmodifiable set in Java.
Unmodifiable sets are “read-only” wrappers over other collections. They do not support any modification operations such as add, remove, and clear, but their underlying collection can be changed. Sets that additionally guarantee that no change in the Collection object will ever be visible are referred to as immutable.
It is worth noting that making a set final will not make it unmodifiable. We can still add elements or remove elements from it. Only the reference to the set is final.
1. Using Collections.unmodifiableSet() method
Collections unmodifiableSet(Set s) returns an unmodifiable “read-only” view of the specified set. Any attempt to modify the returned set directly or using an iterator will result in an UnsupportedOperationException. However, any changes made to the original set will be reflected in the unmodifiable set.
Please note that Collections also provides an unmodifiableSortedSet(SortedSet s) method that returns an unmodifiable view of the specified sorted set.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.HashSet; import java.util.Arrays; import java.util.Collections; import java.util.Set; class Main { // Unmodifiable set in Java public static void main(String[] args) { Set<String> mutableSet = new HashSet<>(Arrays.asList("C", "C++", "Java")); // Using Java Collections Set<String> unmodifiableSet = Collections.unmodifiableSet(mutableSet); try { // any attempt to modify the set will result in // an `UnsupportedOperationException` unmodifiableSet.add("C#"); } catch (UnsupportedOperationException ex) { System.out.println("java.lang.UnsupportedOperationException"); } // if we modify the original set, the changes will be reflected // back in the unmodifiableSet mutableSet.remove("C"); // remove `C` mutableSet.add("Go"); // add `Go` System.out.println(unmodifiableSet); } } |
Output:
java.lang.UnsupportedOperationException
[Java, C++, Go]
2. Using Collections.unmodifiableCollection() method
Collection interface offers another method, unmodifiableCollection(Set s), which returns an unmodifiable view of the specified collection. This is similar to unmodifiableSet() except it returns a Collection instead of a Set.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.*; class Main { // Unmodifiable set in Java public static void main(String[] args) { Set<String> mutableSet = new HashSet<>(Arrays.asList("C", "C++", "Java")); // Using Java Collections Collection<String> unmodifiableSet = Collections .unmodifiableCollection(mutableSet); try { // any attempt to modify the collection will result in // an `UnsupportedOperationException` unmodifiableSet.add("C#"); } catch (UnsupportedOperationException ex) { System.out.println("java.lang.UnsupportedOperationException"); } // if we modify the original set, the changes will be reflected // back in the unmodifiableSet mutableSet.remove("C"); // remove `C` mutableSet.add("Go"); // add `Go` System.out.println(unmodifiableSet); } } |
Output:
java.lang.UnsupportedOperationException
[Java, C++, Go]
3. Using Apache Commons Collections
Apache Commons Collections SetUtils class provides unmodifiableSet(Set s) that returns an unmodifiable set backed by the given set. If the given set is null, it throws a NullPointerException. The set will throw an UnsupportedOperationException if any modification operation is performed on it. However, any changes made to the original set will be reflected in the returned set.
Please note that Apache Commons Collections SetUtils class also provides the unmodifiableSortedSet(SortedSet s) method that returns an unmodifiable sorted set backed by the given sorted set.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import org.apache.commons.collections4.SetUtils; import java.util.HashSet; import java.util.Arrays; import java.util.Set; class Main { // Unmodifiable set in Java public static void main(String[] args) { String[] lang = new String[] { "C", "C++", "Java" }; Set<String> mutableSet = new HashSet<>(Arrays.asList(lang)); // Apache Commons Collections – creates a view Set<String> unmodifiableSet = SetUtils.unmodifiableSet(mutableSet); try { // any attempt to modify the set will result in // an `UnsupportedOperationException` unmodifiableSet.add("C#"); } catch (UnsupportedOperationException ex) { System.out.println("java.lang.UnsupportedOperationException"); } // if we modify the original set, the changes will be reflected // back in the unmodifiableSet mutableSet.remove("C"); // remove `C` mutableSet.add("Go"); // add `Go` System.out.println(unmodifiableSet); } } |
Output:
java.lang.UnsupportedOperationException
[Java, C++, Go]
That’s all about unmodifiable Set in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)