Lists class by Guava in Java
This article will discuss Guava Lists class, which provides several static utility methods pertaining to List instances in Java.
The Lists class is one of the most commonly used class in Guava, which offers the following static utility methods:
1. Using Lists.asList() method
We can’t insert a new element to an array as the array size is fixed in Java. One solution to this problem is to allocate a new array of +1 the size of the original array, and copy all elements from the original array to the new array. This works, but it is highly inefficient. Guava provides an efficient solution to this problem: The Lists.asList() method returns an unmodifiable list backed by the original array, which also contains the specified element. This is useful for adding an element to an array without creating a new array.
|
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 |
import com.google.common.collect.Lists; import java.util.List; class Main { public static void main(String[] args) { int first = 10; int second = 20; Integer[] rest = { 30, 40, 50 }; // The list contains 'first' element, followed by elements of // the 'rest' array List<Integer> firstList = Lists.asList(first, rest); System.out.println(firstList); // [10, 30, 40, 50] // The list contains the 'first' and 'second' element, followed by // elements of the 'rest' array List<Integer> secondList = Lists.asList(first, second, rest); System.out.println(secondList); // [10, 20, 30, 40, 50] // List contains elements of the 'rest' array List<Integer> list = Lists.asList(null, rest) .subList(1, rest.length + 1); // [30, 40, 50] System.out.println(list); } } |
2. Using Lists.charactersOf() method
Guava’s Lists.charactersOf() method returns a view of the specified string as an immutable list of Character values. No actual copying happens here as this method just returns a view and throws an UnsupportedOperationException if we try to modify the list. This method is often used to iterate over the characters of a string using a for-each loop or an iterator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.google.common.collect.Lists; import java.util.List; class Main { public static void main(String[] args) { String string = "Hello"; // immutable list of Character values List<Character> chars = Lists.charactersOf(string); // using the for-each loop for (Character ch: chars) { System.out.println(ch); // H, e, l, l, o } } } |
3. Using Lists.newArrayList() method
Guava library provides the Lists.newArrayList() utility method that returns a mutable ArrayList instance containing elements from the specified iterable. This is a convenient way to create a list from an array, a collection, or any other iterable. To get an immutable list, Guava provides the ImmutableList.copyOf() method.
|
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 |
import com.google.common.collect.Lists; import java.util.Arrays; import java.util.Iterator; import java.util.List; class Main { public static void main(String[] args) { // 1. Create an empty mutable `ArrayList` List<Integer> emptyList = Lists.newArrayList(); System.out.println(emptyList); // [] // 2. Create a mutable `ArrayList` from elements of the given array Integer[] ints = { 1, 2, 3 }; List<Integer> list = Lists.newArrayList(ints); System.out.println(list); // [1, 2, 3] // 3. Create a mutable `ArrayList` from elements of the given iterable Iterable<Integer> iterable = Arrays.asList(1, 2, 3, 4); List<Integer> collection = Lists.newArrayList(iterable); System.out.println(collection); // [1, 2, 3, 4] // 4. Create a mutable `ArrayList` from the given iterator Iterator<Integer> itr = Arrays.asList(1, 2, 3, 4, 5).iterator(); List<Integer> mutableList = Lists.newArrayList(itr); System.out.println(mutableList); // [1, 2, 3, 4, 5] } } |
4. Using Lists.reverse() method
Guava Lists class also provides the reverse() utility method that returns a reversed view of the specified list. The returned list is backed by the original list, so any changes in the returned list are reflected in the original list and vice-versa.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.collect.Lists; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> mutableList = Arrays.asList(1, 2, 3, 4, 5); List<Integer> reverse = Lists.reverse(mutableList); System.out.println(reverse); // [5, 4, 3, 2, 1] } } |
5. Using Lists.transform() method
Guava’s Lists.transform() method returns a list after applying a specified method to each element of the original list. The transformation happens in such a way that any changes to the original list will be reflected in the returned list, but no new elements can be added to the returned list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.Lists; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); // convert list of Integer to list of String List<String> newList = Lists.transform(list, String::valueOf); System.out.println(newList); // [1, 2, 3, 4, 5] } } |
6. Using Lists.partition() method
Guava’s Lists.partition() method divides a list into sublists of the same size, which are just views of the original list. The final list may be smaller. This is useful for splitting a large list into smaller sublists.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.collect.Lists; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<Integer> mutableList = Arrays.asList(1, 2, 3, 4, 5); List<List<Integer>> lists = Lists.partition(mutableList, 2); System.out.println(lists); // [[1, 2], [3, 4], [5]] } } |
That’s all about Lists class by Guava in Java.
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 :)