Sets class by Guava in Java
This article will discuss Sets class by Guava in Java.
The Sets class by Guava is a Java library that provides several static utility methods for working with sets. It can help we create, manipulate, and perform operations on sets in a convenient and efficient way. To use the Guava Sets class, we need to import the com.google.common.collect.Sets package. Some of the methods that the Sets class offers are:
1. Using Sets.newHashSet() method
Guava library provides the Sets.newHashSet() method is a convenient way to create a mutable HashSet instance in Java. It returns a mutable HashSet instance containing distinct elements from the specified array, iterable or iterator as the argument. Guava also has Sets.newConcurrentHashSet() method, which returns a thread-safe Set backed by a HashMap. Here’s an example of using these methods:
|
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 26 27 28 29 30 31 32 33 |
import com.google.common.collect.Sets; import java.util.Arrays; import java.util.Iterator; import java.util.Set; class Main { public static void main(String[] args) { // 1. Create an empty mutable `HashSet` Set<Integer> emptySet = Sets.newHashSet(); System.out.println(emptySet); // [] // 2. Create a mutable `HashSet` from elements of the given array Integer[] ints = { 1, 2, 3 }; Set<Integer> Set = Sets.newHashSet(ints); System.out.println(Set); // [1, 2, 3] // 3. Create a mutable `HashSet` from elements of the given iterable Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4); Set<Integer> collection = Sets.newHashSet(iterable); System.out.println(collection); // [1, 2, 3, 4] // 4. Create a `ConcurrentHashSet` from elements of the given iterable Set<Integer> concurrentHashSet = Sets.newConcurrentHashSet(iterable); System.out.println(concurrentHashSet); // [1, 2, 3] // 5. Create a mutable `HashSet` from the given iterator Iterator<Integer> itr = Arrays.asList(1, 2, 3, 4, 5).iterator(); Set<Integer> mutableSet = Sets.newHashSet(itr); System.out.println(mutableSet); // [1, 2, 3, 4, 5] } } |
2. Using Sets.newTreeSet() method
To create a TreeSet instance, Guava provides the Sets.newTreeSet() method that sorts by the natural sort ordering of its elements or with the given comparator. The TreeSet can be initialized with the elements of an iterable. Guava also provides Sets.newLinkedHashSet() method that creates a mutable LinkedHashSet instance. Here’s an example of using these methods:
|
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 26 27 28 29 |
import com.google.common.collect.Sets; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Set; class Main { public static void main(String[] args) { // 1. Create an empty mutable `TreeSet` Set<Integer> emptySet = Sets.newTreeSet(); System.out.println(emptySet); // [] // 2. Create a mutable `TreeSet` from elements of the given iterable List<Integer> iterable = Arrays.asList(1, 2, 3, 4); Set<Integer> collection = Sets.newTreeSet(iterable); System.out.println(collection); // [1, 2, 3, 4] // 3. Create a mutable `LinkedHashSet` from elements of the given iterable Set<Integer> linkedHashSet = Sets.newLinkedHashSet(iterable); System.out.println(linkedHashSet); // [1, 2, 3, 4] // 4. Create an empty mutable `TreeSet` with the given comparator Set<Integer> set = Sets.newTreeSet(Comparator.reverseOrder()); set.addAll(iterable); System.out.println(set); // [4, 3, 2, 1] } } |
3. Using Sets.filter() method
The Guava Sets.filter() method returns a set containing elements of the specified set which satisfies a predicate. Any changes made to the returned set affects the original set and vice-versa. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.base.Predicates; import com.google.common.collect.Sets; import java.util.Set; class Main { public static void main(String[] args) { Set<Integer> first = Sets.newHashSet(1, 2, 3, 4, 5); Set<Integer> second = Sets.newHashSet(1, 3, 5, 7); Set<Integer> filteredSet = Sets.filter(first, Predicates.in(second)); System.out.println(filteredSet); // [1, 3, 5] } } |
4. Using Sets.difference() method
The Guava Sets.difference() method returns an unmodifiable view of the two sets’ difference. The returned set contains all elements that are present in the first set and not present in the second set. Guava also has Sets.symmetricDifference() method, which gives a view that cannot be modified and shows the elements that are in one set or the other, but not both. Here’s an example of using these methods:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.google.common.collect.Sets; import java.util.Set; class Main { public static void main(String[] args) { Set<Integer> first = Sets.newHashSet(1, 2, 3, 4, 5, 6, 7, 8); Set<Integer> second = Sets.newHashSet(1, 3, 5, 7, 9, 10); Set<Integer> difference = Sets.difference(first, second); System.out.println(difference); // [2, 4, 6, 8] Set<Integer> symmetricDifference = Sets.symmetricDifference(first, second); System.out.println(symmetricDifference); // [2, 4, 6, 8, 9, 10] } } |
5. Using Sets.intersection() method
The Guava Sets.intersection() method returns an unmodifiable view of the intersection of two sets. The returned set contains all elements that are present in both sets. Here is an example of using this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.collect.Sets; import java.util.Set; class Main { public static void main(String[] args) { Set<Integer> first = Sets.newHashSet(1, 2, 3, 4, 5); Set<Integer> second = Sets.newHashSet(3, 5, 7, 9); Set<Integer> intersection = Sets.intersection(first, second); System.out.println(intersection); // [3, 5] } } |
6. Using Sets.union() method
The Guava Sets.union() method returns an unmodifiable view of the union of two sets. The returned set contains all elements that are present in either set. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.collect.Sets; import java.util.Set; class Main { public static void main(String[] args) { Set<Integer> first = Sets.newHashSet(1, 2, 4, 5, 6); Set<Integer> second = Sets.newHashSet(3, 5, 7); Set<Integer> union = Sets.union(first, second); System.out.println(union); // [1, 2, 4, 5, 6, 3, 7] } } |
7. Using Sets.powerSet() method
The Guava Sets.powerSet() method returns the set of all possible subsets of a set. A power set of a set S is the set of all possible subsets of S, including the empty set and S itself. Here is an example of using this method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.collect.Sets; import java.util.Set; class Main { public static void main(String[] args) { Set<Integer> set = Sets.newHashSet(1, 2, 3); Set<Set<Integer>> powerSet = Sets.powerSet(set); for (Set<Integer> s: powerSet) { System.out.println(s); } } } |
Output:
[]
[1]
[2]
[1, 2]
[3]
[1, 3]
[2, 3]
[1, 2, 3]
That’s all about Sets 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 :)