ImmutableSet class by Guava in Java
This post will talk about ImmutableSet class by Guava in Java.
Guava is a popular Java library that provides many useful utilities and data structures, including immutable collections. One of the immutable collections that Guava provides is ImmutableSet, which is a subclass of the AbstractSet class, which implements the Set interface. The ImmutableSet class is immutable, which means that we cannot add, remove, or modify its elements once it is created. This offer several benefits, such as thread-safety, performance, and avoiding errors. There are several static methods in the ImmutableSet class of Guava that can help us work with immutable sets in Java. The following sections explain these methods in detail:
1. Using ImmutableSet.of() method
The Guava ImmutableSet.of() is a convenient way to create a small set with known elements. It returns an immutable set having the given elements. The ImmutableSet.of() method has seven different versions that can handle different numbers of elements. The last version uses var-args to accept any number of elements. To create an instance of ImmutableSet, we can use one of the following overloaded methods:
static <E> ImmutableSet<E> of(E e)
static <E> ImmutableSet<E> of(E e1, E e2)
static <E> ImmutableSet<E> of(E e1, E e2, E e3)
static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4)
static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5)
static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E… others)
The ImmutableSet class does not allow null elements. If we try to add a null element to an immutable set, we will get a NullPointerException. Also, the ImmutableSet class preserves the insertion order of the elements. This means that the iteration order of an immutable set is the same as the order in which we added the elements. Here is a program to demonstrate the usage of the Guava’s ImmutableSet.of() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.google.common.collect.ImmutableSet; import java.util.Set; class Main { public static void main(String[] args) { Set<Integer> first = ImmutableSet.of(); System.out.println(first); // [] Set<Integer> second = ImmutableSet.of(1, 2, 3, 4, 5); System.out.println(second); // [1, 2, 3, 4, 5] Integer[] arr = { 7, 8, 9, 10 }; Set<Integer> third = ImmutableSet.of(1, 2, 3, 4, 5, 6, arr); System.out.println(third); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } } |
2. Using ImmutableSet.Builder<E>
Guava also provides a builder class that allows to add elements to an immutable set in a fluent way. It is helpful to create a large or complex set that cannot be easily specified with the of() method. To use the builder, we need to create an instance of it, either by calling its constructor or by using the ImmutableSet.builder() static method. Then, we can add elements to the builder using the add() or addAll() methods. Finally, we can call the build() method to get the immutable set. The following code illustrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.ImmutableSet; import java.util.Arrays; class Main { public static void main(String[] args) { ImmutableSet<Integer> set = ImmutableSet.<Integer>builder() .addAll(Arrays.asList(1, 2, 3)) .add(4) .add(5) .build(); System.out.println(set); // [1, 2, 3, 4, 5] } } |
3. Using ImmutableSet.copyOf() method
The Guava ImmutableSet.copyOf() method returns an immutable set containing elements of the specified collection, or a non-primitive array, or an iterator. It is useful when we want to make an immutable copy of an existing collection that might be mutable. The following code example demonstrates its usage:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import com.google.common.collect.ImmutableSet; import java.util.Arrays; import java.util.HashSet; import java.util.Set; class Main { public static void main(String[] args) { Integer[] ints = { 1, 2, 3, 4, 5 }; Set<Integer> mutableSet = new HashSet<>(Arrays.asList(ints)); // Create an immutable set from elements of the given array ImmutableSet<Integer> a = ImmutableSet.copyOf(ints); System.out.println(a); // [1, 2, 3, 4, 5] // Create an immutable set from the given mutable set ImmutableSet<Integer> b = ImmutableSet.copyOf(mutableSet); System.out.println(b); // [1, 2, 3, 4, 5] // Create an immutable set from the given iterator ImmutableSet<Integer> c = ImmutableSet.copyOf(mutableSet.iterator()); System.out.println(c); // [1, 2, 3, 4, 5] } } |
4. Using ImmutableSet.contains() method
The Guava ImmutableSet.contains() method finds if the target element is present in the immutable set or not. Its working is similar to the HashSet.contains() method. The performance of the ImmutableSet.contains() method will depend on the immutable set implementation, but it will be lower than the mutable HashSet. We can use the ImmutableSet.contains() method like this to check if the immutable set contains a given element:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import com.google.common.collect.ImmutableSet; class Main { public static void main(String[] args) { ImmutableSet<Integer> immutableSet = ImmutableSet.of(1, 2, 3, 4, 5); int target = 2; if (immutableSet.contains(target)) { System.out.println("The target element is present in the set"); } else { System.out.println("The target element is not present in the set"); } } } |
5. Using ImmutableSet.iterator() method
The Guava ImmutableSet.iterator() method returns an unmodifiable iterator across the elements in the Set. Its working is similar to the AbstractSet.iterator() method. Here is an example of how to use this method:
|
1 2 3 4 5 6 7 8 9 10 11 |
import com.google.common.collect.ImmutableSet; class Main { public static void main(String[] args) { ImmutableSet<Integer> immutableSet = ImmutableSet.of(1, 2, 3, 4, 5); immutableSet.iterator().forEachRemaining(System.out::println); } } |
That’s all about ImmutableSet class by Guava 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 :)