Guava Lists.newArrayList() method in Java
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 emptyArrayList.Lists.newArrayList(E… elements): Creates anArrayListcontaining the given elements in order.Lists.newArrayList(Iterable<? extends E> elements): Creates anArrayListcontaining the elements of the given iterable in order.Lists.newArrayList(Iterator<? extends E> elements): Creates anArrayListcontaining 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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import com.google.common.collect.Lists; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; class Main { public static void main(String[] args) { // Create an empty ArrayList List<String> emptyList = Lists.newArrayList(); System.out.println(emptyList); // [] // Create an ArrayList containing "a", "b", "c" List<String> listFromIndividualElements = Lists.newArrayList("a", "b", "c"); System.out.println(listFromIndividualElements); // [a, b, c] // Create an ArrayList containing the elements of another list List<String> listFromAnotherList = Lists.newArrayList(Arrays.asList("d", "e", "f")); System.out.println(listFromAnotherList); // [d, e, f] // Create an ArrayList containing the elements of a set Set<String> set = new HashSet<>(Arrays.asList("p", "q", "r")); List<String> listFromSet = Lists.newArrayList(set); System.out.println(listFromSet); // [p, q, r] // Create an ArrayList containing the elements of an iterator List<String> listFromIterator = Lists.newArrayList(set.iterator()); System.out.println(listFromIterator); // [p, q, r] } } |
The Lists class also provides Lists.newArrayListWithCapacity(int initialArraySize) method to create an instance of an empty ArrayList with the specified initial capacity.
|
1 2 3 4 5 6 7 8 9 10 |
import com.google.common.collect.Lists; import java.util.List; class Main { public static void main(String[] args) { // Create an empty ArrayList with initial capacity of 10 List<String> listWithCapacity = Lists.newArrayListWithCapacity(10); System.out.println(listWithCapacity); // [] } } |
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
ArrayListand 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 ofncopies 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.
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 :)