Java List Reverse reverseIterable(final List list)

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

Description

Allows a foreach loop to iterate backwards over a list from the end to the start.

License

Open Source License

Exception

Parameter Description
NullPointerException if list is null

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  w ww. ja v  a 2 s.com*/
 *     Mike Kucera (IBM Corporation) - initial API and implementation
 *******************************************************************************/

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

public class Main {
    /**
     * Allows a foreach loop to iterate backwards over a list
     * from the end to the start.
     * 
     * e.g.
     * for(Object o : reverseIterable(list)) { ... }
     * 
     * @throws NullPointerException if list is null
     */
    public static <T> Iterable<T> reverseIterable(final List<T> list) {
        return iterable(reverseIterator(list));
    }

    /**
     * Creates an Iterable instance that just returns
     * the given Iterator from its iterator() method.
     * 
     * This is useful for using an iterator in a foreach loop directly.
     * 
     * e.g.
     * 
     * for(Object o : iterable(list.listIterator())) {
     *     // do something
     * }
     * 
     * @throws NullPointerException if list is null
     */
    public static <T> Iterable<T> iterable(final Iterator<T> iter) {
        if (iter == null)
            throw new NullPointerException("iter parameter is null"); //$NON-NLS-1$

        return new Iterable<T>() {
            public Iterator<T> iterator() {
                return iter;
            }
        };
    }

    /**
     * Returns an iterator that iterates backwards over the given list.
     * The remove() method is not implemented and will throw UnsupportedOperationException.
     * The returned iterator does not support the remove() method.
     * 
     * @throws NullPointerException if list is null
     */
    public static <T> Iterator<T> reverseIterator(final List<T> list) {
        return new Iterator<T>() {
            ListIterator<T> iterator = list.listIterator(list.size());

            public boolean hasNext() {
                return iterator.hasPrevious();
            }

            public T next() {
                return iterator.previous();
            }

            public void remove() {
                throw new UnsupportedOperationException("remove() not supported"); //$NON-NLS-1$
            }
        };
    }
}

Related

  1. reverseCopy(List original)
  2. reversed(final List list)
  3. reversed(List l)
  4. reversed(List list)
  5. reversedCopy(List src, List dest)
  6. reverseIterator(final List list)
  7. reverseIterator(List list)
  8. reverseIterator(ListIterator iterator)
  9. reverseList(final List list)