CodingBison

Iterator is one of the key concepts of Java Collection Framework. An 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.

Things To Remember
Unlike Enumeration, Iterator has the capability to delete the elements of a Collection during the walk itself.

An Iterator could be used 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 walking, An Enumeration does not have this capability.

Java Collection Framework has an interface called List, where the elements can be accessed using the indices/positions. List interface also provides wide variety of methods which operates on the 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. We will discuss List Interface and List Iterators in the next section.

Methods defined in Iterator interface

Iterator interface defines those methods with which, we can walk though the Collection easily and also efficiently. Here is the list of the methods defined in the Iterator interface.


Table: Methods defined in Iterator Interface
MethodDescription
bool hasNext()Returns True if the Iterator has a next element, else returns False
Object next()Returns next element of the Iterator. Returns an exception if no element exists
void remove()This method needs to be called, only after a call to the "next()". This is because remove method deletes the entry, that was returned last by the Iterator.

Here is an example program that demonstrates the usage of all the above mentioned operations. The example have used ArrayList and demonstrated the usage of the methods defined by the Iterator interface.

 import java.util.ArrayList;
 import java.util.Iterator;

 public class IteratorDemo {
     public static void main(String[] args) {
  	ArrayList<Integer> alistObj = new ArrayList<Integer>();
  	alistObj.add(10);
  	alistObj.add(120);
  	alistObj.add(130);
  	alistObj.add(140);
  	Iterator iterObj = alistObj.iterator();
  	System.out.println (" Original List: " + alistObj);
  	while (iterObj.hasNext()) {
  	    System.out.println (" Next Element from the List: " + iterObj.next());
  	    iterObj.remove();
         }
         System.out.println (" Final List: " + alistObj);
     }
 }

Let us Compile/run the program. Here is the output:

  Original List: [10, 120, 130, 140]
  Next Element from the List: 10
  Next Element from the List: 120
  Next Element from the List: 130
  Next Element from the List: 140
  Final List: []




comments powered by Disqus