Guava ImmutableSet.iterator() method in Java
In this post, we have learn how to use the Guava ImmutableSet.iterator() method in Java to get an unmodifiable iterator over the elements in an immutable set. We will also see some of the benefits of using this method over other alternatives.
1. Overview of iterator() method
The Guava ImmutableSet.iterator() method is a method that returns an unmodifiable iterator over the elements in an immutable set. An iterator is an object that allows us to traverse a collection of elements one by one, without knowing the internal structure or representation of the collection. An unmodifiable iterator is an iterator that does not support the remove() operation, which would otherwise modify the underlying collection.
The iterator() method is inherited from the AbstractSet class, which implements the Set interface. The Set interface extends the Iterable interface, which defines the iterator() method. Therefore, any class that implements Set must also implement iterator(). The ImmutableSet class overrides this method to return a custom iterator that is optimized for its internal representation. The iterator() method has the following signature:
|
1 |
public abstract UnmodifiableIterator<E> iterator() |
The type parameter E represents the element type of the set. It returns an object of type UnmodifiableIterator<E>, which is a subclass of Iterator<E> that throws an UnsupportedOperationException if the remove() method is called.
2. Usage of iterator() method
To use the iterator() method, we need to have an instance of ImmutableSet first. We can create an immutable set using one of the static factory methods provided by the ImmutableSet class, such as of(), copyOf(), or builder(). Once we have an immutable set, we can call the iterator() method on it to get an iterator over its elements. For example, you can use the ImmutableSet.iterator() method to iterate over an immutable set of strings as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import com.google.common.collect.ImmutableSet; import java.util.Iterator; class Main { public static void main(String[] args) { // Create an immutable set using the of() method ImmutableSet<String> fruits = ImmutableSet.of("avocado", "banana", "cherry"); // Get an iterator over fruits Iterator<String> fruitIterator = fruits.iterator(); // Loop through fruits using a while loop while (fruitIterator.hasNext()) { String fruit = fruitIterator.next(); System.out.println(fruit); } } } |
Note that we cannot use the remove() method on the iterator, as it will throw an UnsupportedOperationException. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import com.google.common.collect.ImmutableSet; import java.util.Iterator; class Main { public static void main(String[] args) { // Create an immutable set using the of() method ImmutableSet<String> fruits = ImmutableSet.of("avocado", "banana", "cherry"); // Get an iterator over fruits Iterator<String> fruitIterator = fruits.iterator(); // Try to remove an element using the iterator try { fruitIterator.remove(); } catch (UnsupportedOperationException e) { System.out.println("Cannot remove from an immutable set"); } // prints "Cannot remove from an immutable set" } } |
3. Benefits of using the iterator() method
The iterator() method has several advantages over other ways of accessing or manipulating the elements in an immutable set. Some of them are:
- It is consistent and predictable. The
iterator()method preserves the order of the elements in the set, which is determined by their first occurrence in the source or by their natural order if they are comparable. This means that we can rely on the iteration order to be stable and meaningful. - It is safe and efficient. The
iterator()method guarantees that the returned iterator is unmodifiable and thread-safe. It also avoids unnecessary copying or hashing of the elements if they are already stored in a compact or optimized way internally. - It is flexible and expressive. The
iterator()method allows us to traverse the elements in any way we want, without being constrained by the internal structure or representation of the set. We can also use theiterator()method in conjunction with other methods or classes that accept an iterator as a parameter.
That’s all about Guava ImmutableSet.iterator() method in Java. If you are interested in learning more about this method, you can check out the Guava official website or its GitHub repository.
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 :)