List builder java

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

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

1. Mutable lists

Lists.newArrayList[] creates a mutable ArrayList instance containing the given elements. We should use this method 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.

1
List mutableList = Lists.newArrayList["C", "C++", "Java"];

2. Immutable lists

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

Guavas 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 modified operation such as adding new elements, removing an element, or changing an element, will throw an UnsupportedOperationException.

⮚ ImmutableList.copyOf[]

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

1
ImmutableList immutableList = ImmutableList.copyOf[Arrays.asList["C", "C++"]];

⮚ ImmutableList.Builder[]

Guava also provides a builder for creating an immutable list instance using Builders addAll[] method, as shown below:

1
2
3
ImmutableList immutableList = new ImmutableList.Builder[]
.addAll[Arrays.asList["C", "C++"]]
.build[];


In this version as well, were basically creating a list from another list. We can also directly add specified elements to the ImmutableList without constructing any intermediary list using Builders add[] method. It takes a single element at a time instead of a complete list.

1
2
3
4
ImmutableList immutableList = ImmutableList.builder[]
.add["C"]
.add["C++"]
.build[];


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

⮚ ImmutableList.of[]

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

1
List immutableList = ImmutableList.of["C", "C++", "Java"];


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 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.

Heres 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 the above-mentioned methods to create mutable and immutable empty list instances in Java.

⮚ Lists.newArrayList[]

1
List mutableList = Lists.newArrayList[];

⮚ ImmutableList.copyOf[]

1
ImmutableList immutableList = ImmutableList.copyOf[Lists.newArrayList[]];

⮚ ImmutableList.Builder[]

1
ImmutableList immutableList = new ImmutableList.Builder[].build[];

⮚ ImmutableList.of[]

1
List immutableList = ImmutableList.of[];

4. Mutable List from ImmutableList

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

1
2
List mutableList = Lists.newArrayList[ImmutableList.copyOf[
Arrays.asList["C", "C++"]]];
1
2
3
List mutableList = Lists.newArrayList[new ImmutableList.Builder[]
.addAll[Arrays.asList["C", "C++"]]
.build[]];
1
2
3
4
List mutableList = Lists.newArrayList[ImmutableList.builder[]
.add["C"]
.add["C++"]
.build[]];
1
List mutableList = Lists.newArrayList[ImmutableList.of["C", "C++", "Java"]];

Thats all about initializing List in Java using the Guava library.


Related Post:

Initialize List in Java in a single line

Video liên quan

Chủ Đề