Java Guava | Lists.reverse() method with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

[5, 4, 3, 2, 1]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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-



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.