Java : ListIterator with Examples
ListIterator (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the ListIterator<E> methods.
Summary
An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list.
final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var iterator = list.listIterator();
System.out.println("-- next --");
while (iterator.hasNext()) {
final var value = iterator.next();
System.out.println(value);
}
// Result
// ↓
//-- next --
//aaa
//bbb
//ccc
System.out.println("-- previous --");
while (iterator.hasPrevious()) {
final var value = iterator.previous();
System.out.println(value);
}
// Result
// ↓
//-- previous --
//ccc
//bbb
//aaa
Methods
void add (E e)
Inserts the specified element into the list (optional operation).
final var list = new ArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var iterator = list.listIterator();
while (iterator.hasNext()) {
final var value = iterator.next();
iterator.add(value.toUpperCase());
}
System.out.println(list); // [aaa, AAA, bbb, BBB, ccc, CCC]
boolean hasNext ()
Returns true if this list iterator has more elements when traversing the list in the forward direction.
final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var iterator = list.listIterator();
System.out.println("-- next --");
while (iterator.hasNext()) {
final var index = iterator.nextIndex();
final var value = iterator.next();
System.out.printf("index = %d, value = %s%n", index, value);
}
// Result
// ↓
//-- next --
//index = 0, value = aaa
//index = 1, value = bbb
//index = 2, value = ccc
boolean hasPrevious ()
Returns true if this list iterator has more elements when traversing the list in the reverse direction.
final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var iterator = list.listIterator(list.size());
System.out.println("-- previous --");
while (iterator.hasPrevious()) {
final var index = iterator.previousIndex();
final var value = iterator.previous();
System.out.printf("index = %d, value = %s%n", index, value);
}
// Result
// ↓
//-- previous --
//index = 2, value = ccc
//index = 1, value = bbb
//index = 0, value = aaa
E next ()
Returns the next element in the list and advances the cursor position.
final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var iterator = list.listIterator();
System.out.println("-- next --");
while (iterator.hasNext()) {
final var index = iterator.nextIndex();
final var value = iterator.next();
System.out.printf("index = %d, value = %s%n", index, value);
}
// Result
// ↓
//-- next --
//index = 0, value = aaa
//index = 1, value = bbb
//index = 2, value = ccc
int nextIndex ()
Returns the index of the element that would be returned by a subsequent call to next().
final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var iterator = list.listIterator();
System.out.println("-- next --");
while (iterator.hasNext()) {
final var index = iterator.nextIndex();
final var value = iterator.next();
System.out.printf("index = %d, value = %s%n", index, value);
}
// Result
// ↓
//-- next --
//index = 0, value = aaa
//index = 1, value = bbb
//index = 2, value = ccc
E previous ()
Returns the previous element in the list and moves the cursor position backwards.
final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var iterator = list.listIterator(list.size());
System.out.println("-- previous --");
while (iterator.hasPrevious()) {
final var index = iterator.previousIndex();
final var value = iterator.previous();
System.out.printf("index = %d, value = %s%n", index, value);
}
// Result
// ↓
//-- previous --
//index = 2, value = ccc
//index = 1, value = bbb
//index = 0, value = aaa
int previousIndex ()
Returns the index of the element that would be returned by a subsequent call to previous().
final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var iterator = list.listIterator(list.size());
System.out.println("-- previous --");
while (iterator.hasPrevious()) {
final var index = iterator.previousIndex();
final var value = iterator.previous();
System.out.printf("index = %d, value = %s%n", index, value);
}
// Result
// ↓
//-- previous --
//index = 2, value = ccc
//index = 1, value = bbb
//index = 0, value = aaa
void remove ()
Removes from the list the last element that was returned by next() or previous() (optional operation).
final var list = new ArrayList<Integer>();
Collections.addAll(list, 1, 2, 3, 4, 5, 6);
System.out.println(list); // [1, 2, 3, 4, 5, 6]
final var iterator = list.listIterator();
while (iterator.hasNext()) {
final var value = iterator.next();
if (value % 2 == 0) {
iterator.remove();
}
}
System.out.println(list); // [1, 3, 5]
void set (E e)
Replaces the last element returned by next() or previous() with the specified element (optional operation).
final var list = new ArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
final var iterator = list.listIterator();
while (iterator.hasNext()) {
final var value = iterator.next();
iterator.set(value.toUpperCase());
}
System.out.println(list); // [AAA, BBB, CCC]
Methods declared in Iterator
Related posts
- API Examples