Java 9 was released around march 2017 and please install jdk 9, it will be helpful in understanding the code used in this article. In Java 9, there are some features added in Java language and factory method for immutable Set is one of them.
So lets get started!
Characteristics of Immutable Set:
- As the name suggest these Set are immutable.
- If any attempt made to add, delete and update elements in the set we will have UnsupportedOperationException.
- Immutable set do not allow null element either.
- If any attempt made to create a immutable set with null element, we will have NullPointerException. If any attempt made to add null element in set, we will have UnsupportedOperationException.
Creating immutable set in java 8
In java 8 to create immutable set, we use java.util.Collections.unmodifiableSet(Set set) method. This method returns an unmodifiable view of the specified set. This method allows modules to provide users with “read-only” access to internal sets.
Syntax: public static Set unmodifiableSet(Set set) Returns: an unmodifiable view of the specified set. Exception: NA
Java code for immutable empty and non-empty set in java 8:
// Java code illustrating immutable set in java 8 import java.util.*; class GfG { public static void main(String args[]) { // creating empty set Set<String> s1 = new HashSet<String>(); Set<String> us1 = Collections.unmodifiableSet(s1); // creating non-empty set Set<String> s2 = new HashSet<String>(); s2.add( "contribute.geeksforgeeks.org" ); Set us2 = Collections.unmodifiableSet(s2); System.out.println(us1); System.out.println(us2); } } |
Output:
[] [contribute.geeksforgeeks.org]
Now lets try adding elements in these immutable set:
// Java code illustrating immutable set in java 8 import java.util.*; class GfG { public static void main(String args[]) { // creating empty set and unmodifiable set HashSet<String> s1 = new HashSet<String>(); Set<String> us1 = Collections.unmodifiableSet(s1); // creating non-empty set and unmodifiable set HashSet<String> s2 = new HashSet<String>(); s2.add( "contribute.geeksforgeeks.org" ); Set<String> us2 = Collections.unmodifiableSet(s2); // try adding new element us1.add( "gfg" ); us2.add( "ide.neveropen" ); } } |
Above code will generate exception, because we are trying to add element in immutable set.
Runtime Error : Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055) at ImmutableListDemo.main(File.java:16)
Creating immutable set in java 9
To create immutable set in java 9, we use of() method.
Java code to create immutable set in java 9:
// Java code illustrating of() method import java.util.Set; class GfG { public static void main(String args[]) { // empty immutable set Set<String> is1 = Set.of( "ide" , "code" , "practice" ); System.out.println(is1); } } |
Output:
[ide.code.practice]
Now lets try adding elements in these immutable set:
// Java code illustrating of() method import java.util.*; import com.google.common.collect.ImmutableSet; class GfG { public static void main(String args[]) { // empty immutable set Set<>String is1 = Set.of(); // non-empty immuttable set Set<String> is2 = Set.of( "ide" , "contribute" , "support" ); // Adding an element throws exception is1.add( null ); is2.add( "set" ); } } |
Exception in thread "main" java.lang.UnsupportedOperationException at com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:218) at ImmutableListDemo.main(Main.java:16)
This article is contributed by Abhishek Verma. If you like Lazyroar 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 Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.