Java List Reverse reverseIterator(final List list)

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

Description

Returns an iterator that iterates backwards over the given list.

License

Open Source License

Exception

Parameter Description
NullPointerException if list is null

Declaration

public static <T> Iterator<T> reverseIterator(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 ww w. j av  a2 s.  c o m
 *     Mike Kucera (IBM Corporation) - initial API and implementation
 *******************************************************************************/

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

public class Main {
    /**
     * 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. reversed(final List list)
  2. reversed(List l)
  3. reversed(List list)
  4. reversedCopy(List src, List dest)
  5. reverseIterable(final List list)
  6. reverseIterator(List list)
  7. reverseIterator(ListIterator iterator)
  8. reverseList(final List list)
  9. reverseList(List list)