Initialize a List using Guava in Java
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import com.google.common.collect.Lists; import java.util.List; class Main { public static void main(String[] args) { List<String> mutableList = Lists.newArrayList("C", "C++", "Java"); System.out.println(mutableList); // [C, C++, Java] } } |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import com.google.common.collect.ImmutableList; import java.util.Arrays; class Main { public static void main(String[] args) { ImmutableList<String> immutableList = ImmutableList.copyOf(Arrays.asList("C", "C++")); System.out.println(immutableList); // [C, C++] } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.ImmutableList; import java.util.Arrays; class Main { public static void main(String[] args) { ImmutableList<String> immutableList = ImmutableList.<String>builder() .add("C") .add("C++") .addAll(Arrays.asList("Java", "Python")) .build(); System.out.println(immutableList); // [C, C++, Java, Python] } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import com.google.common.collect.ImmutableList; import java.util.List; class Main { public static void main(String[] args) { List<String> immutableList = ImmutableList.of("C", "C++", "Java"); System.out.println(immutableList); // [C, C++, Java] } } |
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:
|
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.ImmutableList; import com.google.common.collect.Lists; import java.util.List; class Main { public static void main(String[] args) { List<String> mutableList = Lists.newArrayList(); System.out.println(mutableList); // [] ImmutableList<String> immutableList1 = ImmutableList.copyOf(Lists.newArrayList()); System.out.println(immutableList1); // [] ImmutableList<String> immutableList2 = new ImmutableList.Builder<String>().build(); System.out.println(immutableList2); // [] List<String> immutableList3 = ImmutableList.of(); System.out.println(immutableList3); // [] } } |
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:
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 :)