Skip to content
Related Articles

Related Articles

Improve Article

Java Guava | Lists.reverse() method with Examples

  • Last Updated : 04 Feb, 2019

Guava’s Lists.reverse() method accepts a list as a parameter and returns a reversed view of the list passed as the parameter. If the specified list is random access, then the returned list is also random-access.

For example: Lists.reverse(Arrays.asList(1, 2, 3)) returns a list containing 3, 2, 1.

Syntax:

public static <T> List<T> reverse(List<T> list)

Parameter: The method accepts list as a parameter and returns a list which is backed by this list. It means that the changes made in the returned list are reflected back in the list passed as parameter to the method and vice-versa.

Return Value: The method Lists.reverse() returns the reversed view of the list passed as parameter. The returned list supports all of the optional list operations supported by the list passed as parameter.



Below examples illustrate the implementation of above method:

Example 1:




// Java code to show implementation of
// Guava's Lists.reverse() method
  
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // Creating a List of Integers
        List<Integer> myList
            = Arrays.asList(1, 2, 3, 4, 5);
  
        // Using Lists.reverse() method to get a
        // reversed view of the specified list. Any
        // changes in the returned list are reflected
        // in the original list and vice-versa
        List<Integer> reverse = Lists.reverse(myList);
  
        // Displaying the reversed view of specified List
        System.out.println(reverse);
    }
}
Output:
[5, 4, 3, 2, 1]

Example 2:




// Java code to show implementation of
// Guava's Lists.reverse() method
  
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // Creating a List of Characters
        List<Character> myList
            = Arrays.asList('H', 'E', 'L', 'L', 'O');
  
        // Using Lists.reverse() method to get a
        // reversed view of the specified list. Any
        // changes in the returned list are reflected
        // in the original list and vice-versa
        List<Character> reverse = Lists.reverse(myList);
  
        // Displaying the reversed view of specified List
        System.out.println(reverse);
    }
}
Output:
[O, L, L, E, H]

Reference: https://google.github.io/guava/releases/23.0/api/docs/com/google/common/collect/Lists.html#reverse-java.util.List-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :