Initialize a Set using Guava in Java
This post will discuss various methods to initialize a Set using Guava library in Java.
The Guava library provides several static utility methods for creating both mutable and immutable set instances. There are several methods to initialize a Set using Guava in Java:
1. Constructing Mutable Sets
We can create a HashSet instance that can be modified and can contain null using the Sets.newHashSet() method. This method internally uses the HashSet constructor to construct an empty set and then adds the given elements using Collections.addAll(). It does not allow any repeated elements while creating the set.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import com.google.common.collect.Sets; import java.util.Set; class Main { public static void main(String[] args) { Set<String> mutableSet = Sets.newHashSet("NYC", "New Delhi", "Seattle"); System.out.println(mutableSet); // [Seattle, NYC, New Delhi] } } |
2. Constructing Immutable Sets
If we don’t need to modify the Set, we should use Guava’s immutable versions which will not allow any duplicates or null values in the Set (why?). If we try to add a null value, we will get a NullPointerException. If we try to modify the immutable Set in any way, such as adding, removing, or changing an element, we will get an UnsupportedOperationException. We can create an ImmutableSet from a collection by using the ImmutableSet.copyOf() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import com.google.common.collect.ImmutableSet; import java.util.Arrays; class Main { public static void main(String[] args) { ImmutableSet<String> immutableSet = ImmutableSet.copyOf( Arrays.asList("NYC", "New Delhi")); System.out.println(immutableSet); // [NYC, New Delhi] } } |
We can also use the ImmutableSet.Builder method from Guava to get a builder for creating an immutable set instance. The Builder.add() method lets us add individual elements to the immutable set. The Builder.addAll() method allows us to add elements from another collection to the immutable set. For example:
|
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<String> immutableSet = ImmutableSet.<String>builder() .add("NYC") .add("New Delhi") .addAll(Arrays.asList("Seattle", "Tokyo")) .build(); System.out.println(immutableSet); // [NYC, New Delhi, Seattle, Tokyo] } } |
We can also create an ImmutableSet by using the ImmutableSet.of() method, which returns an immutable set containing the given elements. Guava has 7 versions of this method with different numbers of parameters, and one varargs method that can take any number of elements. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import com.google.common.collect.ImmutableSet; import java.util.Set; class Main { public static void main(String[] args) { Set<String> immutableSet = ImmutableSet.of("NYC", "New Delhi", "Seattle"); System.out.println(immutableSet); // [NYC, New Delhi, Seattle] } } |
3. Constructing Empty Sets
To create a mutable or immutable empty set instance in Java, we can use any of the methods we discussed above. The Sets.newHashSet() method can create an empty mutable set instance, and the ImmutableSet.copyOf(), ImmutableSet.Builder(), or ImmutableSet.of() methods can create an immutable empty set instance. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import java.util.Set; class Main { public static void main(String[] args) { Set<String> mutableSet = Sets.newHashSet(); System.out.println(mutableSet); // [] ImmutableSet<String> immutableSet1 = ImmutableSet.copyOf(Sets.newHashSet()); System.out.println(immutableSet1); // [] ImmutableSet<String> immutableSet2 = new ImmutableSet.Builder<String>().build(); System.out.println(immutableSet2); // [] Set<String> immutableSet3 = ImmutableSet.of(); System.out.println(immutableSet3); // [] } } |
If needed, we can also convert the ImmutableSet instance back to a mutable set by using the Sets.newHashSet() method with the immutable set as an argument. That’s all about initializing a Set using Guava in Java.
Related Post:
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 :)