Iterator interface

Iterator is an interface of a collection from which provides an implementation independent method of traversing the elements of a collection. Each collection implements provides its own iterator implementation. A collection provides an iterator which allows sequential access to the elements of a collection.
Method of Iterator interface
  1. public Boolean hasNext():
                 It is used to find out whether there is an element to be traversed or not. Returns true if the collection still has elements left to return.
  2. public Object next():
                 It is used to obtain the next element from the collection and returns the current element. If there are no more elements left to return, it throws a NoSuchElementException.
  3. public Object remove():
                 It is used to remove the element that was returned by the last traversed element to the next() method from the collection. This method throws an exception an IllegalStateException, if the next() method has not yet been called, or when the remove() method has already been called after the last call to the next() method. This method is optional for an iterator, that is, it throws an UnsupportedOperationException if the remove operation is not supported.
Processing element using Iterator Interface
With the help of iterator object, we can access each element in the collection, one element at a time. In general, to use an iterator to cycle through the contents of a collection, follow these three steps:
  1. Create an reference of Iterator Interface for obtain an iterator to the start of the collection by calling the collection’s iterator( ) method.
  2. Set up a loop that makes a call to hasNext( ). The loop will be continue as long as hasNext( ) returns true.
  3. Within the loop, obtain each element by calling next( ).
Example with Java 5 generics for getting element form list or set

An iterator might be used as follows. ArrayList<String> arraylist = new ArrayList<String>(); // . . . Add Strings to arraylist Iterator<String> it = arraylist.iterator(); while( it.hasNext() ) { String str = it.next(); // No downcasting required. System.out.println(str); }
Example of getting element form list or set using enchanced for loop.

for (String str : arraylist) { System.out.println(str); }
Example pre Java 5, with explicit iterator and downcasting

An iterator might be used as follows. ArrayList arraylist = new ArrayList(); // Hold as an object // . . . Add Strings to arraylist Iterator it = arraylist.iterator(); while( it.hasNext() ) { String str =(String) it.next(); // Downcasting is required pre java 5 System.out.println(str); }
Example of Iterator interface
import java.util.List; import java.util.ArrayList; import java.util.Iterator; public class IteratorDemo{ public static void main(String[] args) { // List Creation List list = new ArrayList(); // Adding elements to the List list.add("Welcome"); list.add("Mr."); list.add("Sunil"); list.add("Kumar"); list.add("Singh"); //Display list content using iterator method Iterator it = list.iterator(); System.out.println("Display list content using iterator method :"); while (it.hasNext()) { System.out.print(it.next() + " "); } System.out.println(); //Display list content using foreach loop System.out.println("Display list content using foreach loop :"); for(Object str:list) { System.out.print(str + " "); } System.out.println(); // Display list content using size() and get() method System.out.println("Display list content using size() and get() :"); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println(); } }
Output

Display list content using iterator method :
Welcome Mr. Sunil Kumar Singh
Display list content using foreach loop :
Welcome Mr. Sunil Kumar Singh
Display list content using size() and get() :
Welcome Mr. Sunil Kumar Singh

Download this example
 



 

Latest Tutorial


Most Visited Tutorial