Class ReverseListIterator<E>

java.lang.Object
org.apache.commons.collections4.iterators.ReverseListIterator<E>
All Implemented Interfaces:
Iterator<E>, ListIterator<E>, OrderedIterator<E>, ResettableIterator<E>, ResettableListIterator<E>

public class ReverseListIterator<E> extends Object implements ResettableListIterator<E>
Iterates backwards through a List, starting with the last element and continuing to the first. This is useful for looping around a list in reverse order without needing to actually reverse the list.

The first call to next() will return the last element from the list, and so on. The hasNext() method works in concert with the next() method as expected. However, the nextIndex() method returns the correct index in the list, thus it starts high and reduces as the iteration continues. The previous methods work similarly.

Since:
3.2
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private ListIterator<E>
    The list iterator being wrapped.
    private final List<E>
    The list being wrapped.
    private boolean
    Flag to indicate if updating is possible at the moment.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor that wraps a list.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(E obj)
    Adds a new element to the list between the next and previous elements.
    boolean
    Checks whether there is another element.
    boolean
    Checks whether there is a previous element.
    Gets the next element.
    int
    Gets the index of the next element.
    Gets the previous element.
    int
    Gets the index of the previous element.
    void
    Removes the last returned element.
    void
    Resets the iterator back to the start (which is the end of the list as this is a reversed iterator)
    void
    set(E obj)
    Replaces the last returned element.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.util.Iterator

    forEachRemaining
  • Field Details

    • list

      private final List<E> list
      The list being wrapped.
    • iterator

      private ListIterator<E> iterator
      The list iterator being wrapped.
    • validForUpdate

      private boolean validForUpdate
      Flag to indicate if updating is possible at the moment.
  • Constructor Details

    • ReverseListIterator

      public ReverseListIterator(List<E> list)
      Constructor that wraps a list.
      Parameters:
      list - the list to create a reversed iterator for
      Throws:
      NullPointerException - if the list is null
  • Method Details

    • hasNext

      public boolean hasNext()
      Checks whether there is another element.
      Specified by:
      hasNext in interface Iterator<E>
      Specified by:
      hasNext in interface ListIterator<E>
      Returns:
      true if there is another element
    • next

      public E next()
      Gets the next element. The next element is the previous in the list.
      Specified by:
      next in interface Iterator<E>
      Specified by:
      next in interface ListIterator<E>
      Returns:
      the next element in the iterator
    • nextIndex

      public int nextIndex()
      Gets the index of the next element.
      Specified by:
      nextIndex in interface ListIterator<E>
      Returns:
      the index of the next element in the iterator
    • hasPrevious

      public boolean hasPrevious()
      Checks whether there is a previous element.
      Specified by:
      hasPrevious in interface ListIterator<E>
      Specified by:
      hasPrevious in interface OrderedIterator<E>
      Returns:
      true if there is a previous element
    • previous

      public E previous()
      Gets the previous element. The next element is the previous in the list.
      Specified by:
      previous in interface ListIterator<E>
      Specified by:
      previous in interface OrderedIterator<E>
      Returns:
      the previous element in the iterator
    • previousIndex

      public int previousIndex()
      Gets the index of the previous element.
      Specified by:
      previousIndex in interface ListIterator<E>
      Returns:
      the index of the previous element in the iterator
    • remove

      public void remove()
      Removes the last returned element.
      Specified by:
      remove in interface Iterator<E>
      Specified by:
      remove in interface ListIterator<E>
      Throws:
      UnsupportedOperationException - if the list is unmodifiable
      IllegalStateException - if there is no element to remove
    • set

      public void set(E obj)
      Replaces the last returned element.
      Specified by:
      set in interface ListIterator<E>
      Parameters:
      obj - the object to set
      Throws:
      UnsupportedOperationException - if the list is unmodifiable
      IllegalStateException - if the iterator is not in a valid state for set
    • add

      public void add(E obj)
      Adds a new element to the list between the next and previous elements.
      Specified by:
      add in interface ListIterator<E>
      Parameters:
      obj - the object to add
      Throws:
      UnsupportedOperationException - if the list is unmodifiable
      IllegalStateException - if the iterator is not in a valid state for set
    • reset

      public void reset()
      Resets the iterator back to the start (which is the end of the list as this is a reversed iterator)
      Specified by:
      reset in interface ResettableIterator<E>