1.

Differentiate between Iterator and ListIterator in Java.

Answer»

In Java's Collection framework, iterators are used to obtain elements one by one. It can be used on any TYPE of Collection object. We can execute both read and remove operations using Iterator. Iterator MUST be used whenever we want to iterate elements in all Collection framework implemented interfaces, such as Set, List, Queue, and Deque, as well as all Map interface implemented classes. The only cursor accessible for the entire collection framework is the iterator.

ListIterator is only useful for classes that implement List collections, such as array lists and linked lists. It can iterate in both directions. When we WISH to enumerate List elements, we must use ListIterator. This cursor has additional methods and capabilities than the iterator.

IteratorListIterator
Only has the ability to traverse components in a Collection in a forward direction.In both forward and backward orientations, can traverse components in a Collection.
Iterators cannot be used to obtain INDEXES.It offers methods to get element indexes at any time while traversing List, such as next Index() and previous Index().
It aids in the traversal of Maps, Lists, and Sets.Only List may be traversed, not the other two.
It throws a Concurrent Modification Exception since it can't add elements.At any time, you can quickly add elements to a collection.
next(), remove(), and has Next are some of the Iterator's functions ().next(), previous(), has Next(), has Previous(), and add() are some of the List Iterator's methods


Discussion

No Comment Found