Java Reference ยท Collections
The ListIterator interface
Reference for java.util.ListIterator: move forward and backward through a list, replace, insert and remove elements while iterating.
ListIterator<E> extends Iterator<E> for lists. Besides walking forward it can walk backward, tell you the index of the elements around it, and modify the list as it goes: replace the last returned element with set() or insert a new one with add().
Think of the iterator's position as a cursor that sits between elements. next() returns the element after the cursor and moves it forward; previous() returns the element before the cursor and moves it back. You get one from list.listIterator() or list.listIterator(startIndex).
ListIterator<String> it = list.listIterator(); while (it.hasNext()) { String s = it.next(); it.set(s.toUpperCase()); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public interface ListIterator<E> extends Iterator<E> { boolean hasNext(); E next(); boolean hasPrevious(); E previous(); int nextIndex(); int previousIndex(); void remove(); void set(E e); void add(E e); }
| Method | Description |
|---|---|
hasNext(), next() |
Forward traversal. next() throws NoSuchElementException at the end. |
hasPrevious(), previous() |
Backward traversal. previous() throws NoSuchElementException at the start. |
nextIndex() |
Index of the element next() would return, or the list size at the end. |
previousIndex() |
Index of the element previous() would return, or -1 at the start. |
set(E e) |
Replaces the element last returned by next() or previous(). |
add(E e) |
Inserts the element immediately before the one next() would return (and after the one previous() would return). The cursor moves past it. |
remove() |
Removes the element last returned by next() or previous(). |
Usage notes
set()andremove()apply to the last returned element, and each needs anext()orprevious()call since the previousadd()orremove(). OtherwiseIllegalStateException.add()can be called any number of times in a row, unlikeremove(); each element goes in front of the cursor.- Optional operations:
set,addandremovethrowUnsupportedOperationExceptionon lists that do not support them (List.of,Arrays.asListfor add/remove). - Fail-fast, like
Iterator: changing the list structurally by other means causesConcurrentModificationException. - Cost: on an
ArrayList,addandremoveshift elements (O(n)); on aLinkedListthey areO(1)at the cursor, which is whereListIteratorshines. - Starting position:
listIterator(list.size())starts at the end for a reverse walk. For read-only reverse iteration on Java 21+ you can also uselist.reversed(). - Only lists have it. For sets and queues use plain
Iterator.
Related
- Iterator, List, LinkedList, ArrayList, SequencedCollection
- Lessons: Iterators, LinkedList