What is the difference between Iterator and ListIterator ?
Iterator:-
An iterator over a collection.
By using Iterator we can retrieve the elements from
Collection Object in forward direction only.
We can use Iterator to traverse Set and List and also Map
type of Objects.
Iterator iterator =
Set.iterator();
Iterator iterator =
List.iterator();
Methods in Iterator :
- hashNext()
- next()
- remove()
ListIterator:-
An iterator for lists that allows the programmer to traverse
the list in either direction, modify the list during iteration, and obtain the
iterator's current position in the list.
A ListIterator has no current element; its cursor
position always
lies between the element that would be returned by a call to previous() and the
element that would be returned by a call to next().
But List Iterator can be used to traverse for List type
Objects, but not for Set type of Objects.
ListIterator listIterator
= List.listIterator();
i.e., we can't get List Iterator object from Set interface.
Which allows you to traverse in either directions. That is
List Iterators traverse two directions. So it has another methods hasPrevious()
& previous() other than Iterator.
Methods in ListIterator
- hashNext()
- next()
- previous()
- hashPrevious()
- remove()
- nextIndex()
- previousIndex()
No comments:
Post a Comment