ListIterator in Java

ListIterator in Java

Java  has  an  interface ListIterator. It is an enhanced version of Iterator interface. The purpose of ListIterator is to iterate data in collections. If  we use Iterator , then iteration is allowed only in forward direction. But if we use ListIterator iteration in both directions is possible.That is the difference between Iterator and ListIterator.

Methods in ListIterator.

Now , let us see the methods in ListIterator. There are 9 methods in ListIterator.

1)E next()

    2)boolean hasNext()

3)void remove()

4)boolean hasPrevious()

5)E previous()

6)int nextIndex()

7) int previousIndex()

8)void set(E e)

9) void add(E e)

The first  3 methods are there in Iterator .Others are added in ListIterator.Now let us see an example.

The addeItems() method adds few Strings into an ArrayList.The displayItems() method creates a ListIterator object and by using that object iterating the ArrayList in forward and reverse directions.



Iterating the list








So if we need to iterate collections in both directions ListIterator is suitable.

See Related Topics:

Collections in java

Threading in Java

Tagged with 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">