Difference between Iterator and Listiterator?
Solution 1
The differences are listed in the Javadoc for ListIterator
You can
- iterate backwards
- obtain the iterator at any point.
- add a new value at any point.
- set a new value at that point.
Solution 2
There are two differences:
-
We can use Iterator to traverse Set and List and also Map type of Objects. While a ListIterator can be used to traverse for List-type Objects, but not for Set-type of Objects.
That is, we can get a Iterator object by using Set and List, see here:
By using Iterator we can retrieve the elements from Collection Object in forward direction only.
Methods in Iterator:
hasNext()
next()
remove()
Iterator iterator = Set.iterator(); Iterator iterator = List.iterator();
-
But we get ListIterator object only from the List interface, see here:
where as a ListIterator allows you to traverse in either directions (Both forward and backward). So it has two more methods like
hasPrevious()
andprevious()
other than those of Iterator. Also, we can get indexes of the next or previous elements (usingnextIndex()
andpreviousIndex()
respectively )Methods in ListIterator:
- hasNext()
- next()
- previous()
- hasPrevious()
- remove()
- nextIndex()
- previousIndex()
ListIterator listiterator = List.listIterator();
i.e., we can't get ListIterator object from Set interface.
Reference : - What is the difference between Iterator and ListIterator ?
Solution 3
Iterator is super class of ListIterator.
Here are the differences between them:
- With
iterator
you can move only forward, but withListIterator
you can move backword also while reading the elements. - With
ListIterator
you can obtain the index at any point while traversing, which is not possible withiterator
s. - With
iterator
you can check only for next element available or not, but inlistiterator
you can check previous and next elements. - With
listiterator
you can add new element at any point of time, while traversing. Not possible withiterator
. - With
listiterator
you can modify an element while traversing, which is not possible withiterator
.
Iterator look and feel:
public interface Iterator<E> {
boolean hasNext();
E next();
void remove(); //optional-->use only once with next(),
dont use it when u use for:each
}
ListIterator look and feel:
public interface ListIterator<E> extends Iterator<E> {
boolean hasNext();
E next();
boolean hasPrevious();
E previous();
int nextIndex();
int previousIndex();
void remove(); //optional
void set(E e); //optional
void add(E e); //optional
}
Siva
Updated on January 24, 2021Comments
-
Siva over 3 years
Iterator ite = Set.iterator(); Iterator ite = List.iterator(); ListIterator listite = List.listIterator();
We can use
Iterator
to traverse aSet
or aList
or aMap
. ButListIterator
can only be used to traverse aList
, it can't traverse aSet
. Why?I know that the main difference is that with iterator we can travel in only one direction but with
ListIterator
we can travel both directions. Are there any other differences? And any advantages ofListIterator
overIterator
? -
Joachim Sauer almost 12 yearsAnd the reason why you can't do that with a
Set
is simple: There is not "current point": Elements have no index and there is no usefull way you can add an element "before" or "after" another one. -
Cameron Skinner over 10 yearsThis looks like it is largely cut-and-pasted from kirankumarjava.blogspot.com/2013/06/…. You must always acknowledge the original author.
-
gstackoverflow about 10 years@Peter Lawrey obtain the index at any point - Is it about methods previousIndex() and nextIndex() ?
-
kilonet almost 10 years@gstackoverflow check java.util.List#listIterator(int)
-
Shailesh Pratapwar over 8 years@kilonet instead of obtain the index at any point, should it be phrased like "obtain the iterator at any point" to avoid confusion ?