This article will discuss different ways to create a mutable, unmodifiable, and immutable empty set in Java.

Mutable Sets supports modification operations such as add, remove, and clear on it. Unmodifiable Sets are “read-only” wrappers over other sets. They do not support add, remove, and clear operations, but their underlying set can be modified. Sets that guarantee that no change in the set will ever be visible (even if their underlying set is modified) are referred to as immutable.

Please note that making a set final will not make it unmodifiable or immutable. We can still add elements or remove elements from it. Only the reference to the set is final.

1. Mutable Empty Set

⮚ Using Plain Java

We can simply use the HashSet constructor, which constructs an empty resizable-array implementation of the Set interface. The following code takes advantage of the new “diamond” syntax introduced in Java SE 7.

⮚ Using Guava

Guava’s Sets.newHashSet() creates a mutable, empty HashSet instance. This method is deprecated in Java 7 and above.

⮚ Java 8

We can use the Java 8 Stream to construct empty collections by combining stream factory methods and collectors. Collectors.toSet() returns a Collector that accumulates the input elements into a new Set. To create an empty set, pass an empty array.

 

2. Unmodifiable Empty Set

⮚ Using Collections

Collections unmodifiableSet() returns an unmodifiable view of the specified set.

⮚ Using Apache Collections: SetUtils Class

Apache Commons Collections SetUtils.unmodifiableSet() returns an unmodifiable set backed by the specified set.

 
Both the above methods throw a NullPointerException if the specified set is null. If we try to modify the returned set, the set will throw an UnsupportedOperationException. However, any changes in the original mutable set will be visible in the unmodifiable set.

 

3. Immutable Empty Set

There are several ways to create an immutable empty set in Java. The set will throw an UnsupportedOperationException if any modification operation is performed on it.

⮚ Using Collections

We can use Collections.EMPTY_SET that returns an immutable, serializable, and empty set.

 
The above method might throw an unchecked assignment warning. The following example demonstrates the type-safe way to obtain an empty set:

⮚ In Java 8

We could adapt the Collectors.toSet() collector discussed earlier to always produce an immutable empty set, as shown below:

⮚ Using Guava

Guava provides several static utility methods that can be used to obtain an immutable empty set.

1. ImmutableSet.copyOf returns an immutable empty set if the specified set is empty.

 
The method will throw a NullPointerException if the specified set is null, and any changes in the underlying mutable set will not be visible in the immutable set.

 
2. Guava also provides a builder that can create an immutable empty set instance similarly.

 
3. ImmutableSet.of() can also be used to return an immutable empty set.

⮚ Using Apache Collections

Apache Commons Collections provides SetUtils.EMPTY_SET that returns immutable empty set.

⮚ In Java 9

Java 9 comes with static factory methods on the Set interface that can create compact, unmodifiable instances. We can use the Set.of() to create an immutable empty set. The set will throw an UnsupportedOperationException if any modification operation is performed on it.

That’s all about mutable, unmodifiable, and immutable empty Set in Java.