Create Immutable Set in Java

Immutable Set is a Set which immutable.

An ImmutableSet does not allow the null element either.

If we try to create an ImmutableSet with a null element, NullPointerException will be thrown. If any attempt made to add the null element in the set, UnsupportedOperationException will be thrown.

You cannot add or delete objects to it, but you can modify the objects stored it.

The advantage of any immutable collection (Set, Map, List) is thread-safety. We need not want to add thread safety code.

We will see the example of how to create the immutable set in Java

Using Collections.unmodifiableSet

package com.javacodestuffs.core.interfaces.set; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class ImmutableSetExample { public static void main(String args[]) { Set set = new HashSet(); // HashSet set.add(1234); set.add(5555); set.add(6543); set.add(7890); System.out.println(set); //Conver set object to immutable Set immutableSet = Collections.unmodifiableSet(set); immutableSet.add(4523); } } output: [1234, 7890, 5555, 6543] Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055) at com.javacodestuffs.core.interfaces.set.ImmutableSetExample.main(ImmutableSetExample.java:21)

Using the copyOf() method in Guava We use an existing Set to create an ImmutableSet

We have maven dependency

com.google.common google-collect 1.0-rc1

package com.javacodestuffs.core.interfaces.set; import java.util.*; import com.google.common.collect.ImmutableSet; import java.io.*; class ImmutableSetExample1 { public static void main(String args[]) { // creating empty set Set set = new HashSet(); set.add("Summer"); set.add("Rainy"); set.add("Winter"); // An immutable copy of set Set immutableSet = ImmutableSet.copyOf(set); System.out.println(immutableSet); } } output: [Summer, Rainy,Winter]

Using Java 9 Factory Of() method

In Java, if we use of with Set, Map, or List, an Immutable Set is created.

package com.javacodestuffs.core.interfaces.set; import java.util.*; public class ImmutableSetJava9 { public static void main(String args[]) { // non-empty immutable set Set immutableSet = Set.of("Hello", "World"); System.out.println(immutableSet); } } output: [World, Hello]

If we try to change an ImmutableSet? It throws UnsupportedOperationException

package com.javacodestuffs.core.interfaces.set; import java.util.*; public class ImmutableSetJava9 { public static void main(String args[]) { // non-empty immutable set Set immutableSet = Set.of("Hello", "World"); System.out.println(immutableSet); immutableSet.add(null); immutableSet.add("Welcome"); } } output: [Hello, World] Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:71) at java.base/java.util.ImmutableCollections$AbstractImmutableSet.add(ImmutableCollections.java:281) at ImmutableSetJava9.main(ImmutableSetJava9.java:14)

Post/Questions related to Create Immutable Set in Java

How to create immutable map in java?
How to create immutable list in java?
Why wrapper classes are immutable in java?
Guava immutable set from list

In this article, we have seen how to  Create an Immutable Set in Java with examples. All source code in the article can be found in the GitHub repository.