Java List Reverse reverseList(final List list)

Here you can find the source of reverseList(final List list)

Description

Reverses the given list for iteration.

License

Open Source License

Parameter

Parameter Description
T The content type.
list The list to reverse.

Return

The reversed list.

Declaration

public static <T> Iterable<T> reverseList(final List<T> list) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Main {
    /**// w  w w  .  ja  va2 s  . c  om
     * Reverses the given list for iteration. The actual list is not modified.
     *
     * @param <T> The content type.
     * @param list The list to reverse.
     * @return The reversed list.
     */
    public static <T> Iterable<T> reverseList(final List<T> list) {
        return new Iterable<T>() {

            @Override
            public Iterator<T> iterator() {
                final ListIterator<T> li = list.listIterator(list.size());
                return new Iterator<T>() {

                    @Override
                    public boolean hasNext() {
                        return li.hasPrevious();
                    }

                    @Override
                    public T next() {
                        return li.previous();
                    }

                    @Override
                    public void remove() {
                        li.remove();
                    }

                };
            }

        };
    }
}

Related

  1. reversedCopy(List src, List dest)
  2. reverseIterable(final List list)
  3. reverseIterator(final List list)
  4. reverseIterator(List list)
  5. reverseIterator(ListIterator iterator)
  6. reverseList(List list)
  7. reverseList(List list)
  8. reverseList(List list, boolean shallowCopy)
  9. reverseList2(List list)