Guava Lists.reverse() method in Java
In this post, we will explore how to reverse a list in Java using the Guava Lists.reverse() method. We will see how it works, how to use it, and what are its advantages and disadvantages compared to other methods. We will also see some examples of code snippets that demonstrate its usage.
1. Overview of Lists.reverse() method
There are several ways to reverse a list in Java, but one of the most elegant and efficient ones is to use the Lists.reverse() method by Guava. It takes a list as a parameter and returns a reversed view of the list passed as the parameter. A reversed view means that the returned list is backed by the original list, and any changes made to either one are reflected in the other. For example, if you add or remove an element from the reversed list, the original list will also be modified accordingly.
The Guava Lists.reverse() method does not create a copy of the list, nor does it modify the order of elements in the original list. It simply returns a new list object that has a different access pattern to the underlying elements. This makes it very efficient in terms of memory and performance, as it does not require any extra space or time to reverse the list. The syntax of the Guava Lists.reverse() method is as follows:
|
1 |
public static <T> List<T> reverse(List<T> list) |
The method accepts any type of list as a parameter, and returns a list of the same type and generic parameter. The returned list supports all of the optional list operations supported by the original list, such as add(), remove(), set(), get(), etc.
2. Usage of Lists.reverse() method
To use the Guava Lists.reverse() method, you need to add the Guava library as a dependency to your project. Once you have added Guava to your project, you can import the Lists class from the com.google.common.collect package, which contains the reverse() method. Then, you can simply pass any list object as an argument to the reverse() method, and assign the result to another list variable. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import com.google.common.collect.Lists; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { // Create a list of integers List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); // Reverse the list using Guava List<Integer> reversed = Lists.reverse(numbers); // Print both lists System.out.println("Original list: " + numbers); // [1, 2, 3, 4, 5] System.out.println("Reversed list: " + reversed); // [5, 4, 3, 2, 1] } } |
As you can see, the reversed list contains the same elements as the numbers list, but in reverse order. However, note that both lists are still connected by a common backing array. If we modify one of them, the other one will also change. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.google.common.collect.Lists; import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { // Create a list of integers List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); // Reverse the list using Guava List<Integer> reversed = Lists.reverse(numbers); // Add an element to the original list numbers.add(6); // Print both lists again System.out.println("Original list: " + numbers); // [1, 2, 3, 4, 5, 6] System.out.println("Reversed list: " + reversed); // [6, 5, 4, 3, 2, 1] } } |
As you can see, adding an element to the original list also added it to the reversed list at the opposite position. This behavior can be useful or problematic depending on your use case. If you want to keep both lists independent from each other, you need to create a copy of one of them before reversing it. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import com.google.common.collect.Lists; import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { // Create a list of integers List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); // Reverse the copy of the original list using Guava List<Integer> reversed = Lists.reverse(new ArrayList<>(numbers)); // Add an element to the original list numbers.add(6); // Print both lists again System.out.println("Original list: " + numbers); // [1, 2, 3, 4, 5, 6] System.out.println("Reversed list: " + reversed); // [5, 4, 3, 2, 1] } } |
As you can see, the reversed list is not affected by the changes made to the original list. However, note that creating a copy of the list requires extra memory and time, which can be a drawback if the list is large or frequently modified.
3. Advantages and Disadvantages of Lists.reverse() method
The Guava Lists.reverse() method has several advantages and disadvantages compared to other methods of reversing a list in Java. Some of the advantages are:
- It is simple and concise to use. You only need to pass a list as an argument and get a reversed view of it.
- It is efficient and memory-friendly. It does not create a copy of the list or modify the order of elements in the original list. It only returns a new list object that accesses the elements in reverse order.
- It is compatible with any type of list. It works with any implementation of the
Listinterface, such asArrayList,LinkedList,ImmutableList, etc. - It preserves all operations supported by the original list. You can still perform
add(),remove(),set(),get(), etc. on the reversed list as you would on the original list.
Some of the disadvantages of Lists.reverse() method are:
- It creates a dependency on the Guava library. You need to add Guava as a dependency to your project and import the
Listsclass to use thereverse()method. - It creates a bidirectional link between the original and reversed lists. Any changes made to either one are reflected in the other. This can be useful or problematic depending on your use case.
4. Conclusion
The Guava Lists.reverse() method is a powerful and elegant way to reverse a list in Java without creating a copy or modifying the order of elements in the original list. It is simple, efficient, compatible, and flexible. However, it also has some drawbacks, such as creating a dependency on the Guava library, creating a bidirectional link between the original and reversed lists, etc.
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 :)