Immutable Set in Java

In this post, we will discuss various methods to create immutable set in Java. Immutable Sets have many advantages over their mutable siblings, like they are thread-safe, more memory-efficient and can be passed to untrusted libraries without any side effects.


 

Unmodifiable Sets are “read-only” wrappers over other collections. They do not support any modification operations such as add, remove and clear but their underlying collection can be changed. Sets that additionally guarantee that no change in the Collection object will ever be visible are referred to as immutable.

When you don’t expect to modify a collection, it’s a good practice to defensively copy it into an immutable collection. Immutable Sets have many advantages over their mutable siblings, like they are thread-safe, more memory-efficient and can be passed to untrusted libraries without any side effects.

The Collections framework provides unmodifiableSet() method, but it is unsafe to use as the returned set is only truly immutable if nobody holds a reference to the original collection. The returned set is also inefficient as the data structures will still have all the overhead of mutable collections, including concurrent modification checks, extra space in hash tables, etc.

 

1. Guava

Guava provides simple, easy-to-use immutable versions of each standard Collection type, including Guava’s own Collection variations. Unlike Collections’s unmodifiableSet(), which is a view of a separate collection that can still change, an instance of ImmutableSet contains its own private data and will never change.

 
An ImmutableSet collection can be created in several ways:
 

1. Using the copyOf() method

ImmutableSet.copyOf returns an immutable set containing the elements of the specified set. It returns a NullPointerException if any of elements is null.

Java

Download   Run Code

Output:

java.lang.UnsupportedOperationException
[Java, C++, C]

2. Using a Builder

Guava also provides a builder for creating immutable set instances as shown below:

Java

Download   Run Code

Output:

java.lang.UnsupportedOperationException
[C++, C, Java]

3. Using the of method

ImmutableSet.of() returns an immutable set containing the given elements, in order.

Java

Download   Run Code

Output:

java.lang.UnsupportedOperationException
[C++, C, Java]

Please note that Guava also provides ImmutableSortedSet class that has similar methods returning an immutable “sorted” set containing the given elements sorted by their natural ordering.

 

2. Java 9 – Collection factory methods

Several collection factory methods have been added in Java 9 that allow for easy initialization of immutable collections with specified elements. These factory methods are only available for List, Set, and Map Interfaces and the returned collection is structurally immutable. i.e. elements cannot be added, removed, or replaced from the collection. Any modification operation will throw an UnsupportedOperationException. However, if the contained elements are themselves mutable, this may cause the Colection’s contents to appear to change.

Java

Output:

java.lang.UnsupportedOperationException
[C++, C, Java]

 
References: Guava’s Wiki – Immutable Collections Explained

 
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