Java Collection Framework - Iterator Interface� 

Java Collection Framework - Iterator Interface� 


Posted in : Core Java Posted on : January 24, 2011 at 3:53 PM Comments : [ 0 ]

An Iterator is an interface which can be implemented on every collection in a different way. In java, iterator rotates all the elements of a collection sequentially.

Java Collection Framework - Iterator Interface

Introduction to Iterator<E> interface in java

An Iterator is an interface which can be implemented on every collection in a different way. In java, iterator rotates all the elements of a collection sequentially. It performs cycling through all the elements only in one-way. In the older version of java, these works were done by Enumeration interface. It (Enumeration) was an old style of traversing through the elements one by one. In newer version of java (from 1.2), Enumeration interface is replaced by Iterator that is different from Enumerations in the following two ways:

* It allows to remove the elements from the collection during the iteration.

* Upgraded method names.

Syntax

public interface Iterator<E>

In java this interface is used for cycling through all the elements of collection in sequence. In this interface the process of cycling is done in one-way direction. Through this interface we can find the next element, replace the element, remove the element from a collection. The Iterator interface is implemented by iterator( ) method. The iterator( ) method rotates in a collection in sequence one element at a time in forward direction.

Methods of Iterator

An Iterator traverse in one way, and it uses some methods for traversing through all the elements in a collection. For traversing of all the elements. Iterator  requires at least one of the following methods : 

hasNext( ) - While we are using the Iterator, this method is implemented for searching the next element and positioned the cursor at that element in collection. So it returns true if the list has more elements.

next( ) - This method returns the next element in the iteration. So if you want to find that element from collection you may use this method.

remove( ) - This method deletes the last element which is give back by the iterator from the underlying collections.

Implementation of Iterator

Here I will discuss how to implement the Iterator interface.

import java.util.Iterator : This interface provides one-way traversal.

To use an Iterator in any of the collection, follow these steps :

* Find an iterator to the start of the collection by calling the collection's iterator( ) method.

Iterator itr = Collection.iterator( ); // Here Collection is an object of any type of collection.

* Set a loop that makes a call to itr.hasNext( ). It returns true if list contains more element.

*  To find an element from list call itr. next( ) method within the loop.

Example of Iterator interface

In this example we will show you the implementation of Iterator interface and its methods. Here we will show how can the above methods be used with this interface. Through this example we will show you how does a cycling process perform, how can next element be found from a collection.

Example :-

package Devmanuals.com;
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorInterface {
  public static void main(String args[]) {
    ArrayList alist = new ArrayList();
    alist.add("RAJA");
    alist.add("SURAJ");
    alist.add("RAJAK");
    // Implementation of Iterator
    Iterator<String> itr = alist.iterator();
    System.out.println("List has elements = "+itr.hasNext() );
    System.out.println("Elements of list = ");
    while(itr.hasNext())
    {
      System.out.println(itr.next());
    }
    itr.remove();
    System.out.println("After removing element new list = "+alist);    
  }
}

Output :

List has elements = true
Elements of list = 
RAJA
SURAJ
RAJAK
After removing element new list = [RAJA, SURAJ]

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics