Mutable, unmodifiable, and immutable empty Set in Java
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.
|
1 |
Set<String> set = new HashSet<>(); |
⮚ Using Guava
Guava’s Sets.newHashSet() creates a mutable, empty HashSet instance. This method is deprecated in Java 7 and above.
|
1 |
Set<String> set = Sets.newHashSet(); |
⮚ 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.
|
1 |
Set<String> set = Stream.of(new String[]{}).collect(Collectors.toSet()); |
2. Unmodifiable Empty Set
⮚ Using Collections
Collections unmodifiableSet() returns an unmodifiable view of the specified set.
|
1 2 |
Set<String> mutableSet = new HashSet<>(); Set<String> unmodifiable = Collections.unmodifiableSet(mutableSet); |
⮚ Using Apache Collections: SetUtils Class
Apache Commons Collections SetUtils.unmodifiableSet() returns an unmodifiable set backed by the specified set.
|
1 2 |
Set<String> mutableSet = new HashSet<>(); Set<String> unmodifiable = SetUtils.unmodifiableSet(mutableSet); |
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.
|
1 |
Set<String> immutableSet = Collections.EMPTY_SET; |
The above method might throw an unchecked assignment warning. The following example demonstrates the type-safe way to obtain an empty set:
|
1 |
Set<String> immutableSet = Collections.emptySet(); |
⮚ In Java 8
We could adapt the Collectors.toSet() collector discussed earlier to always produce an immutable empty set, as shown below:
|
1 2 3 |
Set<String> immutableSet = Stream.of(new String[] {}) .collect(Collectors.collectingAndThen(Collectors.toSet(), Collections::<String>unmodifiableSet)); |
⮚ 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.
|
1 2 |
Set<String> mutableSet = Sets.newHashSet(); ImmutableSet<String> immutableSet = ImmutableSet.copyOf(mutableSet); |
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.
|
1 |
ImmutableSet<String> immutableSet = new ImmutableSet.Builder().build(); |
3. ImmutableSet.of() can also be used to return an immutable empty set.
|
1 |
Set<String> immutableSet = ImmutableSet.of(); |
⮚ Using Apache Collections
Apache Commons Collections provides SetUtils.EMPTY_SET that returns immutable empty set.
|
1 |
Set<String> immutableSet = SetUtils.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.
|
1 |
Set<String> immutableSet = Set.of(); |
That’s all about mutable, unmodifiable, and immutable empty Set in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)