We would like to know how to reverse an unmodifiable List.
import java.util.AbstractList; import java.util.Arrays; import java.util.List; /*from w w w. ja va 2 s. c o m*/ public class Main { public static void main(String[] args) { String[] array = {"A","B","C","D","E"}; List<String> list = Arrays.asList(array); System.out.println("List : "+list); List<String> reversedView = reversedView(list); System.out.println("Reversed view: "+reversedView); } public static <T> List<T> reversedView(final List<T> list) { return new AbstractList<T>() { @Override public T get(int index) { return list.get(list.size()-1-index); } @Override public int size() { return list.size(); } }; } }
The code above generates the following result.