- 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.
- An ImmutableSet does not allow null element either.
- If any attempt made to create an ImmutableSet with null element, we will have NullPointerException. If any attempt made to add null element in set, we will have UnsupportedOperationException.
- One advantage of any immutable collection (Set, Map, List) is thread safety. These are automatically thread sage as they are immutable.
- Note that it is an immutable collection, not collection of immutable objects, so the objects inside it can be modified.
Creating an ImmutableSet in Java
- Using copyOf() method in Guava We use an existing Set to create an ImmutableSet.
// Creating an immutable set using copyOf()
import
java.util.*;
import
com.google.common.collect.ImmutableSet;
import
java.io.*;
Â
Âclass
GfG
{
   Â
public
static
void
main(String args[])
   Â
{
       Â
// creating empty set
       Â
Set<String> s =
new
HashSet<String>();
       Â
s.add(
"Lazyroar"
);
       Â
s.add(
"Practice"
);
        Â
ÂÂ Â Â Â Â Â Â Â
// An immutable copy of s
       Â
Set<String> is = ImmutableSet.copyOf(s);
          Â
ÂÂ Â Â Â Â Â Â Â
System.out.println(is);
   Â
}
}
Output :
[Lazyroar, Practice]
- Using Of() method in Guava
// Java code illustrating of() method to
// create a ImmutableSet
import
java.util.*;
import
com.google.common.collect.ImmutableSet;
Â
Âclass
GfG
{
   Â
public
static
void
main(String args[])
   Â
{Â Â Â Â Â Â Â Â Â Â
       Â
// non-empty immutable set
       Â
ImmutableSet<String> is =Â
                Â
ImmutableSet.of(
"ide"
,
"code"
);
          Â
ÂÂ Â Â Â Â Â Â Â
// Lets try adding element in these set
       Â
System.out.println(is);Â Â Â Â Â Â Â Â Â Â Â Â Â
   Â
}
}
Output :
[ide, code]
- Using Java 9 Factory Of() method
In Java, if we use of with Set, Map or List, an Immutable Set is created.// Java code illustrating of() method to
// create a ImmutableSet
import
java.util.*;
import
com.google.common.collect.ImmutableSet;
Â
Âclass
GfG
{
   Â
public
static
void
main(String args[])
   Â
{Â Â Â Â Â Â Â Â Â Â
       Â
// non-empty immutable set
       Â
Set<String> is = Set.of(
"ide"
,
"code"
);
          Â
ÂÂ Â Â Â Â Â Â Â
// Let's print the set
       Â
System.out.println(is);Â Â Â Â Â Â Â Â Â Â Â Â Â
   Â
}
}
Output :
[ide, code]
What if we try to change an ImmutableSet?
It throws UnsupportedOperationException
// Java code illustrating of() method import java.util.*;   class GfG {     public static void main(String args[])     {         // empty immutable set         Set<String> is1 = Set.of();                     // non-empty immutable set         Set is2 = Set.of( "ide" , "contribute" , "support" );                     // Lets try adding element in these set         is1.add( null );         is2.add( "set" );                 } } |
Output :
Exception in thread "main" java.lang.UnsupportedOperationException at com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:218) at ImmutableListDemo.main(Main.java:16)
How is it different from Collections.unmodifiableSet()?
Collections.unmodifiableSet creates a wrapper around the same existing set such that the wrapper cannot be used to modify it. However we can still change original Set.
// Java program to demonstrate that a set created using // Collections.unmodifiableSet() can be modified indirectly. import java.io.*; import java.util.*;   class GFG { public     static void main(String[] args)     {         Set<String> s = new HashSet<String>();         s.add( "Geeks" );         Set<String> us = Collections.unmodifiableSet(s);           // We change s and the changes reflect in us.         s.add( "Practice" );         s.add( "Contribute" );         System.out.println(us);     } } |
[Geeks, Practice, Contribute]
If we create an ImmutableSet from an existing set and change the existing set, the Immutable Set does not change because a copy is created.
// Creating an immutable set using copyOf() // and modifying original set. import java.util.*; import com.google.common.collect.ImmutableSet; import java.io.*;   class GfG {     public static void main(String args[])     {         // creating empty set         Set<String> s = new HashSet<String>();         s.add( "Lazyroar" );         s.add( "Practice" );                   // An immutable copy of s         Set<String> is = ImmutableSet.copyOf(s);                     // Now if we change 's', 'is' does not change         s.add( "Contribute" );         System.out.println(is);     } } |
Output :
[Lazyroar, Practice]