147
Iterator ite = Set.iterator();
Iterator ite = List.iterator();

ListIterator listite = List.listIterator();

We can use Iterator to traverse a Set or a List or a Map. But ListIterator can only be used to traverse a List, it can't traverse a Set. 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 of ListIterator over Iterator?

nbro
  • 15,395
  • 32
  • 113
  • 196
Siva
  • 3,297
  • 7
  • 29
  • 35
  • in addition to Peters answer I'd recommend you read a chapter in thinking in java about iterators with all nice examples there – ant Jun 11 '12 at 10:10

4 Answers4

159

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.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 72
    And 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. – Joachim Sauer Jun 11 '12 at 10:11
  • @Peter Lawrey **obtain the index at any point** - Is it about methods *previousIndex()* and *nextIndex()* ? – gstackoverflow Apr 07 '14 at 17:44
  • 2
    @gstackoverflow check java.util.List#listIterator(int) – x__dos May 18 '14 at 11:05
  • 4
    @kilonet instead of **obtain the index at any point**, should it be phrased like "obtain the iterator at any point" to avoid confusion ? – Shailesh Pratapwar Aug 27 '15 at 17:58
43

There are two differences:

  1. 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:

    1. hasNext()
    2. next()
    3. remove()
    Iterator iterator = Set.iterator();
    Iterator iterator = List.iterator();
  2. 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() and previous() other than those of Iterator. Also, we can get indexes of the next or previous elements (using nextIndex() and previousIndex() respectively )

    Methods in ListIterator:

    1. hasNext()
    2. next()
    3. previous()
    4. hasPrevious()
    5. remove()
    6. nextIndex()
    7. 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 ?

Ravi
  • 30,829
  • 42
  • 119
  • 173
jaideep
  • 1,631
  • 17
  • 19
  • 42
    This looks like it is largely cut-and-pasted from http://kirankumarjava.blogspot.com/2013/06/what-is-difference-between-iterator-and.html. You must always acknowledge the original author. – Cameron Skinner Aug 28 '13 at 03:14
28

Iterator is super class of ListIterator.

Here are the differences between them:

  1. With iterator you can move only forward, but with ListIterator you can move backword also while reading the elements.
  2. With ListIterator you can obtain the index at any point while traversing, which is not possible with iterators.
  3. With iterator you can check only for next element available or not, but in listiterator you can check previous and next elements.
  4. With listiterator you can add new element at any point of time, while traversing. Not possible with iterator.
  5. With listiterator you can modify an element while traversing, which is not possible with iterator.

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
}
roottraveller
  • 7,942
  • 7
  • 60
  • 65
user1923551
  • 4,684
  • 35
  • 29
0

the following is that the difference between iterator and listIterator

iterator :

boolean hasNext();
E next();
void remove();

listIterator:

boolean hasNext();
E next();
boolean hasPrevious();
E previous();
int nextIndex();
int previousIndex();
void remove();
void set(E e);
void add(E e);
老衲呢
  • 11
  • 3