Top Description Fields Constructors Methods
org.apache.commons.collections4.iterators

public Class ReverseListIterator<E>

extends Object
implements ResettableListIterator<E>
Class Inheritance
All Implemented Interfaces
org.apache.commons.collections4.ResettableListIterator, org.apache.commons.collections4.OrderedIterator, java.util.Iterator, org.apache.commons.collections4.ResettableIterator, java.util.ListIterator
Imports
java.util.List, .ListIterator, org.apache.commons.collections4.ResettableListIterator

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

Modifier and TypeField and Description
private ListIterator<E>
iterator

The list iterator being wrapped.

private final List<E>
list

The list being wrapped.

private boolean
validForUpdate

Flag to indicate if updating is possible at the moment.

Constructor Summary

AccessConstructor and Description
public
ReverseListIterator(final List<E>
the list to create a reversed iterator for
list
)

Constructor that wraps a list.

Method Summary

Modifier and TypeMethod and Description
public void
add(final E
the object to add
obj
)

Implements java.util.ListIterator.add.

Adds a new element to the list between the next and previous elements.
public boolean

Returns:

true if there is another element
hasNext
()

Implements java.util.ListIterator.hasNext, java.util.Iterator.hasNext.

Checks whether there is another element.
public boolean

Returns:

true if there is a previous element
hasPrevious
()

Implements java.util.ListIterator.hasPrevious, org.apache.commons.collections4.OrderedIterator.hasPrevious.

Checks whether there is a previous element.
public E

Returns:

the next element in the iterator
next
()

Implements java.util.ListIterator.next, java.util.Iterator.next.

Gets the next element.
public int

Returns:

the index of the next element in the iterator
nextIndex
()

Implements java.util.ListIterator.nextIndex.

Gets the index of the next element.
public E

Returns:

the previous element in the iterator
previous
()

Implements java.util.ListIterator.previous, org.apache.commons.collections4.OrderedIterator.previous.

Gets the previous element.
public int

Returns:

the index of the previous element in the iterator
previousIndex
()

Implements java.util.ListIterator.previousIndex.

Gets the index of the previous element.
public void
remove()

Overrides default java.util.Iterator.remove.

Implements java.util.ListIterator.remove.

Removes the last returned element.
public void
reset()

Implements org.apache.commons.collections4.ResettableIterator.reset.

Resets the iterator back to the start (which is the end of the list as this is a reversed iterator)
public void
set(final E
the object to set
obj
)

Implements java.util.ListIterator.set.

Replaces the last returned element.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

iteratorback to summary
private ListIterator<E> iterator

The list iterator being wrapped.

listback to summary
private final List<E> list

The list being wrapped.

validForUpdateback to summary
private boolean validForUpdate

Flag to indicate if updating is possible at the moment.

Constructor Detail

ReverseListIteratorback to summary
public ReverseListIterator(final List<E> list)

Constructor that wraps a list.

Parameters
list:List<E>

the list to create a reversed iterator for

Exceptions
NullPointerException:
if the list is null

Method Detail

addback to summary
public void add(final E obj)

Implements java.util.ListIterator.add.

Adds a new element to the list between the next and previous elements.

Parameters
obj:E

the object to add

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the list is unmodifiable
IllegalStateException:
if the iterator is not in a valid state for set
hasNextback to summary
public boolean hasNext()

Implements java.util.ListIterator.hasNext, java.util.Iterator.hasNext.

Checks whether there is another element.

Returns:boolean

true if there is another element

Annotations
@Override
hasPreviousback to summary
public boolean hasPrevious()

Implements java.util.ListIterator.hasPrevious, org.apache.commons.collections4.OrderedIterator.hasPrevious.

Checks whether there is a previous element.

Returns:boolean

true if there is a previous element

Annotations
@Override
nextback to summary
public E next()

Implements java.util.ListIterator.next, java.util.Iterator.next.

Gets the next element. The next element is the previous in the list.

Returns:E

the next element in the iterator

Annotations
@Override
nextIndexback to summary
public int nextIndex()

Implements java.util.ListIterator.nextIndex.

Gets the index of the next element.

Returns:int

the index of the next element in the iterator

Annotations
@Override
previousback to summary
public E previous()

Implements java.util.ListIterator.previous, org.apache.commons.collections4.OrderedIterator.previous.

Gets the previous element. The next element is the previous in the list.

Returns:E

the previous element in the iterator

Annotations
@Override
previousIndexback to summary
public int previousIndex()

Implements java.util.ListIterator.previousIndex.

Gets the index of the previous element.

Returns:int

the index of the previous element in the iterator

Annotations
@Override
removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Implements java.util.ListIterator.remove.

Removes the last returned element.

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the list is unmodifiable
IllegalStateException:
if there is no element to remove
resetback to summary
public void reset()

Implements org.apache.commons.collections4.ResettableIterator.reset.

Resets the iterator back to the start (which is the end of the list as this is a reversed iterator)

Annotations
@Override
setback to summary
public void set(final E obj)

Implements java.util.ListIterator.set.

Replaces the last returned element.

Parameters
obj:E

the object to set

Annotations
@Override
Exceptions
UnsupportedOperationException:
if the list is unmodifiable
IllegalStateException:
if the iterator is not in a valid state for set