244

I want to have a reversed list view on a list (in a similar way than List#sublist provides a sublist view on a list). Is there some function which provides this functionality?

I don't want to make any sort of copy of the list nor modify the list.

It would be enough if I could get at least a reverse iterator on a list in this case though.


Also, I know how to implement this myself. I'm just asking if Java already provides something like this.

Demo implementation:

static <T> Iterable<T> iterableReverseList(final List<T> l) {
    return new Iterable<T>() {
        public Iterator<T> iterator() {
            return new Iterator<T>() {
                ListIterator<T> listIter = l.listIterator(l.size());                    
                public boolean hasNext() { return listIter.hasPrevious(); }
                public T next() { return listIter.previous(); }
                public void remove() { listIter.remove(); }                 
            };
        }
    };
}

I just have found out that some List implementations have descendingIterator() which is what I need. Though there is no general such implementation for List. Which is kind of strange because the implementation I have seen in LinkedList is general enough to work with any List.

4

12 Answers 12

238

Use the .clone() method on your List. It will return a shallow copy, meaning that it will contain pointers to the same objects, so you won't have to copy the list. Then just use Collections.

Ergo,

Collections.reverse(list.clone());

If you are using a List and don't have access to clone() you can use subList():

List<?> shallowCopy = list.subList(0, list.size());
Collections.reverse(shallowCopy);
7
  • 22
    clone() normally would create a copy of the list. Anyway, List#clone() also does not exist.
    – Albert
    Oct 18, 2010 at 20:31
  • 6
    You are technically right, the List interface itself doesn't provide the clone() method. But ArrayList, LinkedList and Vector all do.
    – jcalvert
    Oct 18, 2010 at 21:15
  • 2
    I just looked up the implementation of clone(). It indeed does a full copy of the list (it only does not clone each single object in the list but that was never what I was talking about).
    – Albert
    Oct 18, 2010 at 23:06
  • 26
    Note that Collections.reverse returns void, so you would lose the clone reference. You need to assign the clone to a variable first, then sort it.
    – user12722
    Apr 16, 2015 at 3:25
  • 15
    subList doesn't copy, it just provides a view on the underlying list, therefore reversing this view will reverse the underlying list.
    – Roland
    Jul 5, 2017 at 5:19
231

Guava provides this: Lists.reverse(List)

List<String> letters = ImmutableList.of("a", "b", "c");
List<String> reverseView = Lists.reverse(letters); 
System.out.println(reverseView); // [c, b, a]

Unlike Collections.reverse, this is purely a view... it doesn't alter the ordering of elements in the original list. Additionally, with an original list that is modifiable, changes to both the original list and the view are reflected in the other.

7
  • 19
    The problem is that Guava is a very large library. See the discussion: github.com/google/guava/issues/1954 and code.google.com/p/guava-libraries/issues/detail?id=605 Jul 15, 2015 at 18:04
  • 3
    @Filipe de Lima Brito: ProGuard is still the best solution for library size, though there are likely improvements we can make. In any case, I don't think library size is relevant in any way to this answer.
    – ColinD
    Jul 15, 2015 at 18:59
  • 3
    Yes, the library size isn’t relevant to this answer, but is relevant to be informed for the programmers (therefore, I commented)! Thank you very much for this great library and for your suggestion @ColinD! Jul 15, 2015 at 19:42
  • @ColinD, yup, it was my mistake. A colleague added google-collections which also have same namespace and class (List) but without reverse method. removing it made guava available again.
    – AaA
    Jul 1, 2016 at 2:44
  • Developers don't always have control over what libraries they can use, and adding a whole new library for something so simple seems like overkill--especially given that the problem can be solved with ListIterator.previous() Oct 17, 2018 at 17:37
81

If i have understood correct then it is one line of code .It worked for me .

 Collections.reverse(yourList);
1
  • 20
    That is not a list view. That modifies the list.
    – Albert
    Jan 6, 2014 at 14:30
43

Its not exactly elegant, but if you use List.listIterator(int index) you can get a bi-directional ListIterator to the end of the list:

//Assume List<String> foo;
ListIterator li = foo.listIterator(foo.size());

while (li.hasPrevious()) {
   String curr = li.previous();
}
1
  • 1
    This is the best answer, since (1) it does not require a library, and (2) it does not modify the original list, as the OP requested Oct 17, 2018 at 17:30
10

I use this:

public class ReversedView<E> extends AbstractList<E>{

    public static <E> List<E> of(List<E> list) {
        return new ReversedView<>(list);
    }

    private final List<E> backingList;

    private ReversedView(List<E> backingList){
        this.backingList = backingList;
    }

    @Override
    public E get(int i) {
        return backingList.get(backingList.size()-i-1);
    }

    @Override
    public int size() {
        return backingList.size();
    }

}

like this:

ReversedView.of(backingList) // is a fully-fledged generic (but read-only) list
8

java.util.Deque has descendingIterator() - if your List is a Deque, you can use that.

1
  • If you didn't want to use built-in descndingIterator() method, it seems like using a ConcurrentLinkedDeque would be the best to reverse a very large list? Basically just copy from one deck to the new deck using poll then offer ? Sorta like just having a deck of cards and taking each off the top into a new pile, in order.
    – djangofan
    Jul 1, 2017 at 18:54
6

Collections.reverse(nums) ... It actually reverse the order of the elements. Below code should be much appreciated -

List<Integer> nums = new ArrayList<Integer>();
nums.add(61);
nums.add(42);
nums.add(83);
nums.add(94);
nums.add(15);
//Tosort the collections uncomment the below line
//Collections.sort(nums); 

Collections.reverse(nums);

System.out.println(nums);

Output: 15,94,83,42,61

2
  • 12
    You're just repeating the answer someone else wrote 6 years ago Oct 17, 2018 at 17:33
  • 2
    ...and not a view. this mutates the list.
    – Jason S
    Nov 22, 2019 at 22:03
3

I know this is an old post but today I was looking for something like this. In the end I wrote the code myself:

private List reverseList(List myList) {
    List invertedList = new ArrayList();
    for (int i = myList.size() - 1; i >= 0; i--) {
        invertedList.add(myList.get(i));
    }
    return invertedList;
}

Not recommended for long Lists, this is not optimized at all. It's kind of an easy solution for controlled scenarios (the Lists I handle have no more than 100 elements).

Hope it helps somebody.

2
  • 2
    Your code have a one problem - you can put any List to it, but it'll always return you ArrayList (as List). And what if I need LinkedList? It's better to modify myList, and return void. Apr 27, 2012 at 12:45
  • 2
    Note that this is not really what I was asking for. I was asking for some sort of proxy/view, not a copy.
    – Albert
    Apr 27, 2012 at 14:26
1

You can also invert the position when you request an object:

Object obj = list.get(list.size() - 1 - position);
1

For small sized list we can create LinkedList and then can make use of descending iterator as:

List<String> stringList = new ArrayList<>(Arrays.asList("One", "Two", "Three"));
stringList.stream().collect(Collectors.toCollection(LinkedList::new))
         .descendingIterator().
         forEachRemaining(System.out::println); // Three, Two, One
System.out.println(stringList); // One, Two, Three
0

Since Java 21 there is a new interface SequencedCollection (which is a superinteface for List). It has the method reversed() which provides a reverse-ordered view of a collection:

jshell> List.of(1, 2, 3, 4, 5).reversed()
$1 ==> [5, 4, 3, 2, 1]
-1

You can also do this:

static ArrayList<String> reverseReturn(ArrayList<String> alist)
{
   if(alist==null || alist.isEmpty())
   { 
       return null;
   }

   ArrayList<String> rlist = new ArrayList<>(alist);

   Collections.reverse(rlist);
   return rlist;
}
2
  • 7
    That is not a list view. A view is the opposite of a copy.
    – Albert
    Jan 15, 2013 at 11:16
  • 5
    The reverse list of an empty list is null??
    – ShellFish
    Jun 20, 2017 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.