What is difference between Iterator and ListIterator?

Answer: Here is the differences and advantages of ListIterator over Iterator.

1-Iterator is available to all Collection classes whereas ListIterator is available to only List classes e.g ArrayList, Linkedlist and Vector.

NOTE:

  • Since List classes are also a collection, so, both Iterator and ListIterator are available to List classes.
  • ListIterator can only be used with list object e.g arraylist , vector and List etc.

2- ListIterator provides extra methods i.e.  hasNext(), hasPrevious(), nextIndex(), previousIndex(), add(Object) and set(Object) etc. So, we will be able to perform below operations with ListIterator that an Iterator cannot perform.

  • Add and modify elements during traversal of elements.
  • Obtain the index at any point during traversal.
  • Traverse elements in both backward and forward directions.

Related Posts