Iterator is one of the key concepts of Java Collection Framework. An Collection Iterator helps in retrieving all the objects by walking through the Collection. As discussed in the Collection Interface section, every Collection has a method "iterator()" that returns an Iterator object.
We can use Iterator to iterate over any type of Collection, as opposed to Enumeration, where the later could be used only over the legacy Collections. One major difference between Iterator and Enumeration is that, Iterator has the capability to even delete the elements of a Collection while Enumeration does not have this Capability.
As we have seen in the earlier section List Interface elements from the List can be accessed/manipulated using an index/position. So, in order to take the advantage of the index in a List, Java Collection Framework provides ListIterator, which iterates over the Collection List using indices/positions. ListIterator extends Iterator and could be used only over a List, while an Iterator could be used over any type of Collection.
ListIterator in addition to defining its own methods, it inherits the methods from Iterator interface. Here is the list of the methods defined in the ListIterator interface. You can imagine this table to be an extension of the table Iterator Interface Methods List.
Method | Description |
---|---|
int nextIndex() | Returns the index of the element that would be returned by the subsequent next() method. |
bool hasPrevious() | Returns True if the Iterator has a previous element, else returns False |
Object previous() | Returns the previous element, else returns -1 if none exits |
int previousIndex() | Returns the index of the element that would be returned by the subsequent previous() method. |
void remove() | This method needs to be called, only after a call to the "next() or previous()". This is because remove method deletes the entry, that was returned last by the Iterator. |
void set(Object o) | This method overwrites the element returned by "next() or previous()". |
Here is an example program that demonstrates the usage of all the above mentioned operations. The below example have used Array List and demonstrated the usage of the methods defined by the Iterator interface.
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class ListIteratorDemo { public static void main(String[] args) { ArrayList<Integer> alistObj = new ArrayList<Integer>(); alistObj.add(13); alistObj.add(17); System.out.println(" Full Original List : " + alistObj); ListIterator<Integer> listIterObj = (ListIterator<Integer>) alistObj.listIterator(); System.out.println(" Get to the End of the List:"); while (listIterObj.hasNext()) { System.out.println("\t Next Index: " + listIterObj.nextIndex() + " Next Element " + listIterObj.next()); } System.out.println(" Get to the Start of the List:"); while (listIterObj.hasPrevious()) { System.out.println(" \t Previous Index: " + listIterObj.previousIndex() + " element: " + listIterObj.previous()); } } }
Let us Compile/run the program, Here is the output:
Full Original List : [13, 17] Get to the End of the List: Next Index: 0 Next Element 13 Next Index: 1 Next Element 17 Get to the Start of the List: Previous Index: 1 element: 17 Previous Index: 0 element: 13