This post will discuss various methods to initialize a list using Guava library in Java.

The Guava library provides a rich list of static utility methods for creating both mutable and immutable list instances. There are several methods to initialize a List using Guava in Java:

1. Creating Mutable Lists

The Lists.newArrayList() creates a mutable ArrayList instance containing the given elements. We can use a mutable list instance if we need to add or remove elements later, or some of the elements can be null. It internally uses the ArrayList constructor to construct an empty list and then calls Collections.addAll(). This method is likely be deprecated in the future.

Download Code

2. Creating Immutable Lists

Guava also provides several simple, easy-to-use immutable versions of List, which should be preferred when mutability is not required. Guava’s immutable list does not allow null values (why?). It will throw a NullPointerException if any element is null. Since the returned list is immutable, any modification operation, such as adding new elements, removing an element, or changing an element, will throw an UnsupportedOperationException. We can use the ImmutableList.copyOf() method to get an instance of ImmutableList containing the elements of the specified collection.

Download Code

 
Guava also provides a builder with ImmutableList.Builder() for creating an immutable list instance using the Builder.addAll() method. We can also directly add specified elements to the ImmutableList without constructing any intermediary collection using the Builder.add() method. It takes a single element at a time instead of a complete list. For example:

Download Code

 
Another way to create an ImmutableList is using the ImmutableList.of() method, which returns an immutable list containing the given elements. Guava provides seven overloaded versions of this method with one varargs method, which can handle any number of elements. For example:

Download Code

 
Note there is a var-args version that can handle any number of elements. The obvious question is: Why Guava has included so many extra methods when only var-args can suffice? The reason is 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. Here’s what a comment in the source says:


// These go up to eleven. After that, we just get the varargs form, and
// whatever warnings might come along with it. 🙁

3. Creating Empty Lists

Finally, we can use any of the above-mentioned methods to create a mutable and immutable empty list instance in Java. The Lists.newArrayList() can be used to create an empty mutable list instance, and any of the ImmutableList.copyOf(), ImmutableList.Builder(), or ImmutableList.of() methods can be used to create an immutable empty list instance. For example:

Download Code

If, for some reason, we want the mutable list back from the ImmutableList instance, we can easily do so by passing it to the Lists.newArrayList() method. That’s all about initializing a List using Guava in Java.

 
Related Post:

Initialize List in Java