Guava Lists.transform() method in Java
In this post, we will focus on one of the most useful methods in the Lists class, which is the transform() method. We will see how to use this method to transform a list based on a function. We will also compare it with the native Java approach and show some examples of code snippets.
1. Overview of transform() method
The Guava Lists.transform() method is a utility method that creates and returns a list that is a transformed view of another list. The method takes a list and a function as parameters, and applies the function to each element of the list. The result is a list that contains the output of the function for each corresponding element of the original list. The syntax of the transform() method is as follows:
|
1 |
public static <F,T> List<T> transform(List<F> fromList, Function<? super F,? extends T> function) |
The type parameter F is the type of elements in the original list. The type parameter T is the type of elements in the transformed list. The parameter fromList is the list to be transformed. The parameter function is the function to apply to each element of the list. The method returns a generic type List<T>, which is an immutable view of the original list with transformed elements.
To use the transform() method, we need to import the com.google.common.collect.Lists class and pass a list and a function as arguments. The function is an object that implements the Function interface, which defines a single abstract method apply() that takes an input and returns an output. For example, if we want to transform a list of integers to strings, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 |
import com.google.common.collect.Lists; import java.util.List; class Main { public static void main(String[] args) { List<Integer> ints = Lists.newArrayList(1, 2, 3); List<String> strings = Lists.transform(ints, String::valueOf); System.out.println(strings); // ["1", "2", "3"] } } |
Note that the transform() method returns an immutable view of the original list, not a copy. This means that any changes made to the original list will be reflected in the transformed list and vice-versa. However, since the transformed list is immutable, we cannot add or remove elements from it.
2. Customize transformation function
The transform() method allows us to customize the transformation function by passing any object that implements the Function interface as an argument. We can use one of the predefined functions provided by Guava or create our own custom function using anonymous classes or lambda expressions. For example, if we want to transform a list of strings to uppercase strings, we can use one of the utility functions from the java.lang.String class with a lambda expression/method reference:
|
1 2 3 4 5 6 7 8 9 10 11 |
import com.google.common.collect.Lists; import java.util.List; class Main { public static void main(String[] args) { List<String> names = Lists.newArrayList("David", "Mia", "Michael"); List<String> upperNames = Lists.transform(names, String::toUpperCase); System.out.println(upperNames); // ["DAVID", "MIA", "MICHAEL"] } } |
Alternatively, we can create our own custom function using an anonymous class:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import com.google.common.base.Function; import com.google.common.collect.Lists; import java.util.List; class Main { public static void main(String[] args) { List<String> names = Lists.newArrayList("David", "Mia", "Michael"); List<String> upperNames = Lists.transform(names, new Function<String, String>() { @Override public String apply(String input) { return input.toUpperCase(); } }); System.out.println(upperNames); // ["DAVID", "MIA", "MICHAEL"] } } |
3. Comparison with native Java approach
The native Java approach for transforming a list is to use the replaceAll() method of the List interface or the map() method of the Stream interface. For example, if we want to transform a list of integers to strings, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5); // using replaceAll() // create a copy to avoid modifying the original list List<Integer> copy = new ArrayList<>(ints); copy.replaceAll(Integer::bitCount); System.out.println(copy); // [1, 1, 2, 1, 2] // using map() List<String> strings = ints.stream() .map(String::valueOf) .collect(Collectors.toList()); System.out.println(strings); // ["1", "2", "3", "4", "5"] } } |
However, there are some challenges and shortcomings of using the native Java approach:
- The
replaceAll()method modifies the original list in place, which can be undesirable if we want to preserve the original data. To avoid this, we have to create a copy of the list before calling the method, which can be costly in terms of memory and performance. Themap()method does not modify the original list, but it creates a new list from the stream, which also requires extra memory and copying. - The
map()method does not return a view of the original list, but returns a new list that is independent of the original list. This means that any changes made to the original list will not be reflected in the transformed list and vice-versa. This can be desirable if we want to isolate the transformed data from the original data, but it can also introduce inconsistency and redundancy. - It requires more boilerplate code. The
replaceAll()andmap()methods require us to use more code to achieve the same functionality as Guava’stransform()method. We have to create a copy or a stream of the original list, apply the transformation function, and collect or assign the result back to a list.
That’s all about Guava Lists.transform() method in Java. If you are interested in learning more about this method, you can check out the Guava official website 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 :)