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

Java How To Find The Average Of 10 Numbers Using A While Loop

What is the Reason to Organize Classes and Interfaces in a Packag ...

What is the Difference Between pass by reference and pass by valu ...

Why Do We Use Tag Library in JSP ?

What are the Differences Between Internal and External Iterator i ...