This post will discuss about Guava Lists.newArrayList() method in Java, which is a static factory method that returns a mutable ArrayList instance containing the given elements.

The ArrayList is one of the most common implementations of the List interface, which provides a dynamic array that can grow and shrink as needed, allows fast random access to its elements, and can contain null values. Guava provides a simpler and more convenient way to create and initialize ArrayLists in Java using Lists.newArrayList() method.

1. Usage of Lists.newArrayList() method

The Lists class contains static factory method Lists.newArrayList(), which creates and returns a mutable ArrayList instance containing the given elements. It has several overloaded versions that accept different types of arguments:

  • Lists.newArrayList(): Creates an empty ArrayList.
  • Lists.newArrayList(E… elements): Creates an ArrayList containing the given elements in order.
  • Lists.newArrayList(Iterable<? extends E> elements): Creates an ArrayList containing the elements of the given iterable in order.
  • Lists.newArrayList(Iterator<? extends E> elements): Creates an ArrayList containing the elements of the given iterator in order.

To use these methods, you need to add Guava as a dependency to your project. Then you can import the Lists class in your code and use the Lists.newArrayList() method to create and initialize ArrayLists. Here are some examples:

Download Code

 
The Lists class also provides Lists.newArrayListWithCapacity(int initialArraySize) method to create an instance of an empty ArrayList with the specified initial capacity.

Download Code

2. Advantages of Lists.newArrayList() method

There are several advantages of using the Guava Lists.newArrayList() method over the standard way of creating and initializing ArrayLists in Java. Here are some of them:

  • It is shorter and more concise. You don’t need to repeat the generic type or use the new keyword. You can also use varargs or iterables to pass multiple elements at once.
  • It is more readable and expressive. You can clearly see that you are creating a new ArrayList and what elements it contains. You can also use method chaining to perform further operations on the list.
  • It is more flexible and consistent. You can use any type of iterable or iterator as an argument, not just arrays or collections. You can also specify the initial capacity of the list to optimize memory usage.

3. Comparing with other methods in Java

Apart from the standard way (using new ArrayList<>()) and the Guava way, there are also other ways to create and initialize ArrayLists in Java. Here are some of them:

  • Using Arrays.asList(): This method returns a fixed-size list backed by an array containing the given elements. It is useful for creating a quick list from an array.
  • Using Collections.nCopies(): This method returns an immutable list consisting of n copies of the specified element. It is useful for creating a list with a fixed size and a default value.
  • Using Stream.of(): This method returns a sequential stream containing the given elements. It is useful for creating a stream from multiple elements, but you need to collect the stream into a list using a collector.
  • Using List.of(): This method returns an immutable list containing the given elements. It is useful for creating a constant list with a few elements, but you need Java 9 or higher to use this method.

4. Conclusion

In this post, we have shown you how to use the Guava Lists.newArrayList() method, which is a static factory method that returns a mutable ArrayList instance containing the given elements. We have also discussed its advantages and covered other ways to create and initialize ArrayLists in Java.

For more information about this method, you can check out the Guava official documentation or its GitHub repository.