Guava Sets.intersection() method in Java
This post will discuss about finding the common elements of two sets in Java using Sets.intersection() method in Java.
A Set collection is used to store and manipulate distinct elements. However, sometimes you might want to find common elements of two sets, also known as the intersection of sets. For example, you might want to compare two sets of keywords, products, or users and see what they have in common. In this post, we will discuss about using the intersection() method of the Sets class in Guava to create a new set that contains the common elements of two sets. We will also discuss the advantages, disadvantages, and functionality of this method, as well as why it is helpful.
1. Overview of intersection() method
The intersection() method of the Sets class returns a new set that contains all elements that are contained in both sets. The method takes two sets as parameters and returns an unmodifiable view of their intersection. The syntax of the method is:
|
1 |
public static <E> Sets.SetView<E> intersection(Set<E> set1, Set<?> set2) |
The type parameter E is the type of elements in the sets. The parameter set1 is the first set, and set2 is the second set. The method returns a view of the intersection of the two sets, which is an unmodifiable set that contains all elements that are contained by both backing sets. The iteration order of the returned set matches that of set1. Note that set2 may also contain elements not present in set1, these are simply ignored.
Here is an example of using the Sets.intersection() method, that returns a set that contains only the elements that are present in both sets.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.Sets; import java.util.Set; class Main { public static void main(String[] args) { // Create two sets of products Set<String> productsA = Sets.newHashSet("Laptop", "Tablet", "Smartphone", "Headphones"); Set<String> productsB = Sets.newHashSet("Smartphone", "Headphones", "Camera", "Speaker"); // Find the common products Set<String> commonProducts = Sets.intersection(productsA, productsB); // Print the common products System.out.println(commonProducts); // [Smartphone, Headphones] } } |
2. Usefulness and Internal Working of intersection() method
The intersection() method is useful when you want to find the common elements of two sets. It allows you to quickly and easily find the shared elements of two sets. You might wonder how the intersection() method works internally, and whether it affects the performance or memory usage of your program.
The intersection() method uses a simple algorithm that iterates over the first set and checks if each element is contained in the second set. This algorithm does not create any intermediate objects. The intersection() method does not affect the memory usage of the final result, as it only returns an unmodifiable view of the intersection. This means that no extra memory is allocated for storing the intersection elements.
3. Benefits and drawbacks of using the intersection() method
Using the intersection() method of the Sets class in Guava has some benefits and drawbacks that you should be aware of before using it. Here are some of the benefits:
Here are some of the drawbacks of using the intersection() method:
- It may be confusing or surprising, as it returns an unmodifiable view of the intersection, meaning that you cannot add or remove any elements from it. This can lead to unexpected or unsupported exceptions if you try to modify it.
- It may be less performant than a direct iteration in some cases, such as when using complex or custom comparators for your sets. This is because the
intersection()method may use a different algorithm that relies on Java’s built-incontains()orremove()methods, which may add some overhead.
4. Conclusion
In this post, we have covered how to use the intersection() method of the Sets class in Guava to find the common elements of two sets. We have also provided an explanation of this method’s utility, how it operates internally, and what are some of its pros and cons.
If you want to learn more about this method, you can check out the Guava documentation and 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 :)