What is the Difference Between an Iterator and ListIterator in Java ?

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.



You May Interest

What is the Purpose of Spring Configuration File ?

How Stack and Heap Work in Java Multi-threading Environment ?

What are the Similarities Between HashSet and HashMap in Java ?

How Will You Make an Object Immutable in Java ?

What are the Situations in Which You Choose HashSet or TreeSet in ...