Computer >> Computer tutorials >  >> Programming >> Java

Difference Between Iterator and ListIterator in Java


In this post, we will understand the difference between Iterator and ListIterator.

Iterator

  • It helps traverse through a map, list and a set.

  • Index can’t be obtained with the help of an iterator.

  • The iterator can’t modify or replace elements of a Collection.

  • It traverses through elements present in Collection.

  • This iteration can be done in the forward direction only.

  • Elements can’t be added, since it would throw ConcurrentModificationException.

  • Methods of the iterator are ‘next()’, ‘remove()’, ‘hasNext()’.

ListIterator

  • It helps traverse through a list only.

  • It can’t traverse through a map and a set.

  • It can traverse through the elements present in Collection.

  • The traversal can be done in both forward and backward directions.

  • Some of the methods of listiterator are ‘nextIndex()’, ‘previousIndex()’, ‘previous()’, ‘next()’.

  • The elements can be modified or replaced.

  • Elements can be added to a collection anytime.