Hi...
Ques : What is an Iterator ? What is the difference between java.util.Iterator and java.util.ListIterator ?
What is an Iterator
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 18, 2012, 9:43 PM EST
The List and Set collections provide iterators, which are objects that allow going over all the elements of a collection in sequence. The
java.util.Iteratorinterface provides for one-way traversal andjava.util.ListIteratorprovides two-way traversal.Iteratoris a replacement for the olderEnumerationclass which was used before collections were added to Java.Creating an Iterator
Iterators are created by calling the iterator() or listIterator() method of a List, Set, or other data collection with iterators.
Iterator Methods
Iterator defines three methods, one of which is optional.
b =it.hasNext()trueif there are more elements for the iterator.obj =it.next()Object, so a downcast was usually required.it.remove()next. Not all collections supportdelete. An UnsupportedOperationException will be thrown if the collection does not support remove().Iterator : Enables you to traverse through a collection in the forward direction only, for obtaining or removing elements ListIterator : extends Iterator, and allows bidirectional traversal of list and also allows the modification of elements.
Thanks