Java Iterator vs ListIterator performance

Java Interview Question Iterator and ListIterator in Java?

PreviousNext

Question

What is the difference between an Iterator and ListIterator in Java?

Answer

Iterator and ListIterator are two interfaces in Java to traverse data structures.

The differences between these two are:

ListIterator can be used to traverse only a List.

But Iterator can be used to traverse List, Set, and Queue etc.

An Iterator traverses the elements in one direction only.

It just goes.

ListIterator can traverse the elements in two directions i.e. backward as well as forward directions.

Iterator cannot provide us index of an element in the Data Structure.

ListIterator provides us methods like nextIndex() and previousIndex() to get the index of an element during traversal.

Iterator does not allow us to add an element to collection while traversing it.

It throws ConcurrentModificationException.

ListIterator allows use to add an element at any point of time while traversing a list.

An existing element's value cannot be replaced by using Iterator.

ListIterator provides the method set(e) to replace the value of last element returned by next() or previous() methods.

PreviousNext

Related

  • Java Interview Question How will you copy elements from a Source List to another list?
  • Java Interview Question P:What are the Java Collection classes that implement List interface?
  • Java Interview Question What are the Java Collection classes that implement Set interface?
  • Java Interview Question Iterator and ListIterator in Java?
  • Java Interview Question difference between Iterator and Enumeration
  • Java Interview Question difference between an ArrayList and a LinkedList data structure?
  • Java Interview Question difference between a Set and a Map in Java