In this article, we will discuss ListIterator interface in detail
1. Key points about ListIterator:
- This is introduced in Java 1.2 version
- Part of Collection framework
- This is applicable only for List classes like ArrayList or LinkedList
2. ListIterator interface:
- ListIterator is a sub-interface of Iterator interface (i.e.; ListIterator extends Iterator)
- This is part of collection framework introduced in Java 1.2 version
- ListIterator interface works only on List objects
- That is, List implemented classes like ArrayList, LinkedList or Vector
- it uses listIterator() method of List interface to iterate over List objects
- It is a bi-directional cursor
- That’s ListIterator allows to iterate over List objects on both direction i.e.; FORWARD as well as BACKWARD directions
- Using ListIterator interface, we can perform addition or replacement of new objects, in addition to read & remove operations
- Signature:
public ListIterator listIterator(); // of List interface
- Present in java.util package and extends java.util.Iterator interface
Q) How to get ListIterator object ?
- We can create ListIterator object using listIterator() method of List interface
- For example,
ListIterator ltr = list.iterator(); // list is a any List object
3. Limitation of ListIterator interface:
- It is applicable only for List objects
- Although ListIterator interface allows to iterate on both FORWARD/BACKWARD directions
4. ListIterator interface methods:
ListIterator methods | Description |
boolean hasNext(); | returns true, if there is a next element\object to be iterated
otherwise returns false, if listIterator reaches end of List |
Object next(); | returns next element/object from List
throws NoSuchElementException, if there isn’t next element |
int nextIndex(); | returns index of next element, if there is a next element
returns size of list, if there isn’t next element |
boolean hasPrevious(); | returns true, if there is a previous element\object to be iterated
otherwise returns false, if listIterator reaches end of List |
Object previous(); | returns previous element/object from List
throws NoSuchElementException, if there isn’t previous element |
int previousIndex(); | returns index of previous element, if there is previous element
returns size of list, if there isn’t previous element |
void remove(); | removes current element from List |
void add(Object obj); | add/inserts new element (next to current object) into List |
void set(Object obj); | replaces current object with new object |
5. ListIterator examples:
ListIteratorExample.java
package in.bench.resources.java.collection;
import java.util.LinkedList;
import java.util.ListIterator;
public class ListIteratorExample {
public static void main(String[] args) {
// creating LinkedList object of type String
LinkedList<String> ll = new LinkedList<String>();
// adding elements to LinkedList object
ll.add("Sundar Pichai");
ll.add("Satya Nadella");
ll.add("Shiv Nadar");
ll.add("Shantanu Narayen");
ll.add("Francisco D’Souza");
// creating ListIterator reference
ListIterator<String> ceo = ll.listIterator();
// FORWARD direction: enumerating using while loop
System.out.println("List Iterating in FORWARD direction\n");
while (ceo.hasNext()){
System.out.println(ceo.next());
}
// BACKWARD direction: enumerating using while loop
System.out.println("\n\nList Iterating in BACKWARD direction\n");
while (ceo.hasPrevious()){
System.out.println(ceo.previous());
}
}
}
Output:
Iterating in FORWARD direction using ListIterator
Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Francisco D’Souza
Iterating in FORWARD direction using ListIterator
Francisco D’Souza
Shantanu Narayen
Shiv Nadar
Satya Nadella
Sundar Pichai
Related Articles:
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/8/docs/api/java/util/List.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html
Happy Coding !!
Happy Learning !!