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:
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
class GFG {
public static void main(String[] args)
{
List<Integer> myList
= Arrays.asList( 1 , 2 , 3 , 4 , 5 );
List<Integer> reverse = Lists.reverse(myList);
System.out.println(reverse);
}
}
|
Example 2:
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
class GFG {
public static void main(String[] args)
{
List<Character> myList
= Arrays.asList( 'H' , 'E' , 'L' , 'L' , 'O' );
List<Character> reverse = Lists.reverse(myList);
System.out.println(reverse);
}
}
|
Reference: https://google.github.io/guava/releases/23.0/api/docs/com/google/common/collect/Lists.html#reverse-java.util.List-