21 Sept 2012

Differences between Iterator and ListIterator in java

Iterator is an interface in the collection framework. Before you can access a collection through an iterator, you must obtain one. Each of the collection classes provides an iterator( ) method that returns an iterator to the start of the collection. By using this iterator object, you can access each element in the collection, one element at a time.
Iterator gives you the ability to access the collection in forward direction.

It has methods hasNext() and next().
hasNext( ) returns false when the end of the list has been reached. Otherwise it returns true.
obtain each element by calling next( ).

ListIterator is an interface in the collection framework. Before you can access a collection through an iterator, you must obtain one. Each of the collection classes provides an listIterator( ) method that returns an iterator to the start of the collection. By using this iterator object, you can access each element in the collection, one element at a time. Iterator gives you the ability to access the collection either in forward direction or backward direction.

It has methods hasNext(), hasPrevious(), next() and previous().
hasNext( ) returns false when the end of the list has been reached. Otherwise it returns true.
hasPrevious() returns true if this list iterator has more elements when traversing the list in the reverse direction. Otherwise it returns false.
obtain each element by calling next( ).
previous() returns the previous element in the list.

0 comments:

Post a Comment