Mutable, Unmodifiable and Immutable Empty Set in Java

In this article, we will discuss different ways to create 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 additionally 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

 

1.1. Using Plain Java –

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

 

1.2. Using Guava –

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

 

1.3. Java 8 –

The Java 8 Stream API can be used 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, we can pass an empty array.

 

2. Unmodifiable Empty Set

 

2.1. Using Collections –

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

 

2.2. Using Apache Collections: SetUtils Class –

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

 
Both above methods throws 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 immutable empty Set in Java. The Set will throw an UnsupportedOperationException if any modify operation is performed on it.

 

3.1. Using Collections –

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

Above method might throw unchecked assignment warning. Below example illustrates the type-safe way to obtain an empty set:

 

3.2. In Java 8 –

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

 

3.3. Using Guava –

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

1. ImmutableSet.copyOf returns an immutable empty set if 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 be used to create immutable empty set instance in similar way.

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

 

3.4. Using Apache Collections –

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

 

3.5. In Java 9 –

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

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz