Initialize Set using Guava in Java

In this post, we will discuss various methods to initialize set using Guava Library in Java.


 

The Google Guava libraries provides a rich set of static utility methods for creating immutable Set instances, including a builder pattern, and also for creating a wide variety of mutable Set instance.

 

1. Mutable Sets –

 
Sets.newHashSet() creates a mutable HashSet instance containing the given elements. This method should be used if you need to add or remove elements later or some of the elements can be null. It internally uses HashSet constructor (with new “diamond” syntax <>) to construct an empty set and then calls Collections.addAll(). Please note that any duplicate elements will be rejected at creation time. This method will likely be deprecated in the future.

 

2. Immutable Sets –

Guava also provides several simple, easy-to-use immutable versions of Set. These should be preferred where mutability is not required.

Guava’s immutable Set will reject duplicate elements at creation time and does not allow null values (why?). A NullPointerException will be thrown if any element is null. Since returned set is immutable, any modify operation such as adding new elements, removing an element or changing an element, will throw an UnsupportedOperationException.

 

2.1. ImmutableSet.copyOf()

ImmutableSet.copyOf returns an immutable set containing the elements of the specified set.

 

2.2. ImmutableSet Builder

Guava also provides a builder for creating immutable set instance using Builder’s addAll() method as shown below:

In this version as well, we are basically creating a set from another set. We can also directly add specified elements to the ImmutableSet without constructing any intermediary set using Builder’s add() method. It takes single element at a time instead of a complete set.

As mentioned earlier, null values are not allowed, and will cause build() to fail.

 

2.3. ImmutableSet.of()

ImmutableSet.of() returns an immutable set containing the given elements, in order. For example,

Guava provides 13 overloaded versions of this method with one varargs method which can handle any number of elements –

static ImmutableSet of()
static ImmutableSet of(E element)
static ImmutableSet of(E e1, E e2)
static ImmutableSet of(E e1, E e2, E e3)
….
….
static ImmutableSet of(E e1, E e2, E e3, E e4, . . . E e10, E e11)
static ImmutableSet of(E e1, E e2, E e3, E e4, . . . E e11, E e12, E… others)

 
Now the obvious question is why Guava has included so many extra methods when only var-args can suffice? The reason is there’s a subtle runtime performance advantage. The var-args version is likely to run slower than the overloadings that do not use varargs. This is because every invocation of a varargs method will cause an array allocation and initialization and not to forget GC overhead.

 

3. Empty Sets –

We can use above methods to create mutable and immutable empty Set instance in Java.

1. Sets.newHashSet()

 
2. ImmutableSet.copyOf

 
3. ImmutableSet.Builder

 
4. ImmutableSet.of()

 

4. Mutable Set from ImmutableSet –

 
If for some reason, we want mutable set back, we can easily do so by using Sets.newHashSet(). For example,

 

 

 

 
 
Related Posts: Initialize Set in Java

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz