Difference between Iterator and ListIterator in Java

In this post, we will see the difference between Iterator and ListIterator in Java. First, we will see Iterator vs ListIterator in brief later we will see each point in details.

IteratorListIterator
Iterator is used to traverse List as well Set.ListIterator is used to traverse List only, we cannot traverse Set using ListIterator.
In case of Iterator we can traverse only forward direction.In case of ListIterator, we can traverse in both the directions(forward as well as backward ).
Iterator has three method.
hasNext()
next()
remove()
ListIterator has following method.
add(E e)
hasNext()
hasPrevious()
next()
nextIndex()
previous()
previousIndex()
remove()
set(E e)
We cannot modify collection while traversing , it will throws ConcurrentModificationException.We can modify collection while traversing.
We don't have any methods in Iterator to get index.We have nextIndex() and previousIndex() in ListIterator.

 

Example of Iterator –

 

package iteratorandlistitearator;
import java.util.*;
public class Example {
public static void main(String[] args) {
	List<String> listObject = new ArrayList<>();
	 
	listObject.add("ram");
	listObject.add("mohan");
	listObject.add("shyam");
	listObject.add("mohan");
	listObject.add("ram");

	// iterator() is a method defined in List interface
	// Iterator is a interface.
	// it is reference of Iterator interface
	Iterator it = listObject.iterator();

	// it.hasNext() will check, next element is there or not if yes while
	// loop will execute.
	while (it.hasNext()) {

		// it.next() will print all element one by one
		System.out.println(it.next());

	}
}
}

 

Output is –

ram
mohan
shyam
mohan
ram

 

Example of ListIterator –

 

package iteratorandlistitearator;
import java.util.*;
public class Example {
public static void main(String[] args) {
	List<String> listObject = new ArrayList<>();
	 
	listObject.add("suresh");
	listObject.add("mohan");
	listObject.add("shyam");
	listObject.add("mohan");
	listObject.add("ram");

	// listIterator() is a method defined in List interface
	// ListIterator is a interface.
	// it is reference of ListIterator interface
	ListIterator it = listObject.listIterator();

	// it.hasNext() will check, next element is there or not if yes while
	// loop will execute.
	System.out.println("iterating using listiterator forword direction ");
	while (it.hasNext()) {

		// it.next() will print all element one by one
		System.out.println(it.next());

	}
	
	System.out.println("iterating using listiterator backword direction ");
	while (it.hasPrevious()) {

		// it.next() will print all element one by one
		System.out.println(it.previous());

	}
}
}

Output is –

iterating using listiterator forword direction
suresh
mohan
shyam
mohan
ram
iterating using listiterator backword direction
ram
mohan
shyam
mohan
suresh

 

We cannot modify a collection while traversing, it will throw ConcurrentModificationException, We can modify a collection while traversing using ListIterator.

package iteratorandlistitearator;
import java.util.*;
public class Example {
public static void main(String[] args) {
	List<String> listObject = new ArrayList<>();
	 
	listObject.add("suresh");
	listObject.add("mohan");
	listObject.add("shyam");
	listObject.add("mohan");
	listObject.add("ram");

	
	Iterator it = listObject.iterator();

	while (it.hasNext()) {

		listObject.add("rohan");
		System.out.println(it.next());

	}
	
	System.out.println(listObject);
		
	
}
}

Output is –

Exception in thread “main” java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at iteratorandlistitearator.Example.main(Example.java:19)


 

package iteratorandlistitearator;
import java.util.*;
public class Example {
public static void main(String[] args) {
	List<String> listObject = new ArrayList<>();
	 
	listObject.add("suresh");
	listObject.add("mohan");
	listObject.add("shyam");
	listObject.add("mohan");
	listObject.add("ram");

	
	ListIterator it = listObject.listIterator();

	while (it.hasNext()) {

		it.add("dd");
		
		System.out.println(it.next());

	}
	
	System.out.println("new list "+listObject);
	
	
	
}
}

 

Output is –

suresh
mohan
shyam
mohan
ram
new list [dd, suresh, dd, mohan, dd, shyam, dd, mohan, dd, ram]


 

Getting index using ListIterator-

 

package iteratorandlistitearator;
import java.util.*;
public class Example {
public static void main(String[] args) {
	List<String> listObject = new ArrayList<>();
	 
	listObject.add("suresh");
	listObject.add("mohan");
	listObject.add("shyam");
	listObject.add("mohan");
	listObject.add("ram");

	
	ListIterator it = listObject.listIterator();

	while (it.hasNext()) {

		
		System.out.println("next index "+it.nextIndex() + " "+"previous index "+it.previousIndex());
		System.out.println(it.next());

	}	
	
}
}

 

Output is –

next index 0 previous index -1
suresh
next index 1 previous index 0
mohan
next index 2 previous index 1
shyam
next index 3 previous index 2
mohan
next index 4 previous index 3
ram

That’s all about Difference between Iterator and ListIterator in Java.

You may like.

Iterator docs.

ListIterator docs.