Initialize List using Guava in Java

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


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


1. Mutable Lists –

Lists.newArrayList() creates a mutable ArrayList 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 ArrayList constructor (with new “diamond” syntax <>) to construct an empty list and then calls Collections.addAll(). This method will likely be deprecated in the future.


2. Immutable Lists –

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

Guava’s immutable List does not allow null values (why?). A NullPointerException will be thrown if any element is null. Since returned list is immutable, any modify operation such as adding new elements, removing an element or changing an element, will throw an UnsupportedOperationException.


1. ImmutableList.copyOf()

ImmutableList.copyOf returns an immutable list containing the elements of the specified list.


2. ImmutableList Builder

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

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

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


3. ImmutableList.of()

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

Guava provides 13 overloaded versions of this method –

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

Note there is a var-args version which can handle any number of elements. 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.

Here is what a comment in the source says:

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


3. Empty Lists –

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

1. Lists.newArrayList()

2. ImmutableList.copyOf

3. ImmutableList.Builder

4. ImmutableList.of()


4. Mutable List from ImmutableList –

If for some reason, we want mutable list back, we can easily do so by using Lists.newArrayList(). For example,




Related Posts: Initialize List 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 🙂

Get great deals at Amazon

Leave a Reply

Notify of