Updated April 4, 2023
Introduction to Java Listiterator
In Java, ListIterator is a bidirectional iterator that helps to iterate elements in a list one-by-one. It is available from version Java 1.2 and extends the interface Iterator. Unlike normal iterator, ListIterator supports CRUD operations, forward and backward iterations. Furthermore, as the position of the cursor is in between elements that will be returned on calling previous() and element that will be returned on calling next (), listiterator does not have a current element.
Syntax
Below is the syntax of Java ListIterator.
ListIterator<T> li = list.listIterator();
How does the ListIterator classwork in Java?
As already discussed, Java ListIterator is a Bi-directional Iterator that traverses in the forward direction and backward direction. For supporting this functionality, two sets of methods are available.
- Forward Direction methods such as hasNext(), next(), nextIndex()
- Backward Direction methods such as hasPrevious(), previous(), previousIndex()
These methods will be discussed in the next section in detail.
Let us see how ListIterator works.
1. Create a list of names
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
nm.add("Elsa");
nm.add("Anna");
nm.add("payal");
2. Create a ListIterator object
//get list iterator
ListIterator<String> li =nm.listIterator();
In the beginning, the Cursor of ListIterator will be pointed before the first element in the List. On calling the methods li.hasNext() and li.next() in a while loop, the cursor will be moved to the last element in the list. In order to start traversing from last, li.hasPrevious( ) and li.previous() methods will be called. On reaching the first element and again calling the same method will return a false value.
Methods of Java Listiterator
The following are the different methods used in Java ListIterator.
1. add(E elm)
Element elm will be inserted into the list. Code:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
public static void main(String a[])
{
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
//add the elements to the list
nm.add("Ram");
nm.add("Jaanu");
nm.add("Elsa");
nm.add("Anna");
nm.add("payal");
//get list iterator
li=nm.listIterator();
System.out.println("List is :");
while(li.hasNext()){
System.out.println(li.next());
}
}
}
Output:
2. hasNext()
True will be returned if the iterator has the next elements during the traversal in the forward direction.
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
public static void main(String a[])
{
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
//get list iterator
li=nm.listIterator();
System.out.println("List is :");
//iterates if next element is present in forward direction
while(li.hasNext()){
System.out.println(li.next());
}
}
}
Output:
3. hasPrevious()
True will be returned if the iterator has the next elements during the traversal in a backward direction.
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
//get list iterator
li=nm.listIterator();
System.out.println("Traversal in forward direction::");
while(li.hasNext()){
System.out.println(li.next());
}
System.out.println("\nTraversal in backward direction:");
while(li.hasPrevious()){
System.out.println(li.previous());
}
}
}
Output:
4. next()
The next element will be returned, and the cursor position will be advanced.
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
public static void main(String a[])
{
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
//get list iterator
li=nm.listIterator();
System.out.println("List is :");
//iterates if next element is present in forward direction
while(li.hasNext()){
System.out.println(li.next());
}
}
}
Output:
5. nextIndex()
The index will be returned for the element that will be returned on calling next().
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
nm.add("Khan");
nm.add("Alia");
nm.add("Jhanvi");
//get list iterator
li=nm.listIterator();
System.out.println("The index of first element : " + li.nextIndex());
li.next();
System.out.println("The index of second element : " + li.nextIndex());
li.next();
System.out.println("The index of third element : " + li.nextIndex());
li.next();
System.out.println("The index of fourth element : " + li.nextIndex());
li.next();
System.out.println("The index of fifth element : " + li.nextIndex());
}
}
Output:
6. previous()
The previous element will be returned, and the cursor position will be moved backward.
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
//get list iterator
li=nm.listIterator();
System.out.println("Traversal in forward direction::");
while(li.hasNext()){
System.out.println(li.next());
}
System.out.println("\nTraversal in backward direction:");
while(li.hasPrevious()){
System.out.println(li.previous());
}
}
}
Output:
7. previousIndex()
The index will be returned for the element that will be returned on calling previous().
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
nm.add("Khan");
nm.add("Alia");
nm.add("Jhanvi");
//get list iterator
li=nm.listIterator();
System.out.println("The index of first element : " + li.previousIndex());
li.next();
System.out.println("The index of second element : " + li.previousIndex());
li.next();
System.out.println("The index of third element : " + li.previousIndex());
li.next();
System.out.println("The index of fourth element : " + li.previousIndex());
li.next();
System.out.println("The index of fifth element : " + li.previousIndex());
} }
Output:
8. remove()
The last element returned on calling next() or previous() will be removed.
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
nm.add("Kavya");
nm.add("Dileep");
nm.add("Sam");
nm.add("Anna");
//get list iterator
li=nm.listIterator();
System.out.println("List is : " + nm);
li.next();
li.next();
li.remove();
System.out.println("After calling remove() method : " + nm);
}
}
Output:
9. set(E e)
Replaces the last element returned by next() or previous() with the specified element (optional operation).
Code:
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIterExample {
//main method
public static void main(String a[])
{
//
ListIterator<String> li = null;
List<String> nm = new ArrayList<String>();
nm.add("Ram");
nm.add("Jaanu");
nm.add("Kavya");
nm.add("Dileep");
nm.add("Sam");
nm.add("Anna");
//get list iterator
li=nm.listIterator();
System.out.println("List is : " + nm);
li.next();
li.next();
li.set("Anjali");
System.out.println("After calling set() method : " + nm);
}
}
Output:
Conclusion
ListIterator is a bidirectional iterator in Java that iterates elements in a list one-by-one. CRUD operations are supported in ListIterator, unlike normal iterator. In this article, different aspects of ListIterator is discussed in detail.
Recommended Articles
This is a guide to Java Listiterator. Here we discuss How does the ListIterator classwork in Java along with the Methods. You may also have a look at the following articles to learn more –