Home‎ > ‎Applied Core Java‎ > ‎Collection Framework‎ > ‎

Iterator

Problem : Create a program that implement Iterator class.

Program :

Step 1 - Coding

Create a text file c:\sunilos\TestIterator.java  and copy below contents.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/*
 * A program to read all elements sequentially from a
 * List with help of Iterator interface
 */
public class TestIterator {

    public static void main(String[] args) {

       
        ArrayList list =  new ArrayList();
        list.add("Basanti-> Kahan  jana hai ");
        list.add("Jay-> Ramgdh jana hai  ");
        list.add("Basanti-> yuki Basanti ko jada bate karne ki aadat to hai nehai..");
        list.add("Basanti-> isaliye  2 rupay lagaga ");
        list.add("Viru-> Tuhara  nam kya hai basanti " );
       
        // Get an iterator from List
        Iterator it = list.iterator();
       
        System.out.println("From Iterator ");
       
        //Check if Iterator has next element
        while (it.hasNext()) {
            //Get next element
            System.out.println(it.next());
        }
    }

}

Step 2 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy TestIterator.java  into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile TestIterator.java with help of 
    • javac TestIterator.java command. Compiled class file will be created in the same folder
  5. Congratulations!! your Java program is ready to serve.

Step 3 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your java program from command prompt with help of 
    • java TestIterator command.

Output


From Iterator
Basanti-> Kahan  jana hai
Jay-> Ramgdh jana hai
Basanti-> yuki Basanti ko jada bate karne ki aadat to hai nehai..
Basanti-> isaliye  2 rupay lagaga
Viru-> Tuhara  nam kya hai basanti

FAQ

What is Iterator?

An object that implements the Iterator interface generates a series of elements, one at a time. Successive calls to the next()method return element in the Iterator.

        Iterator it = list.iterator();
        //Check if Iterator has next element
        while (it.hasNext()) {
            //Get next element
            System.out.println(it.next());
        }
Iterator takes the place of Enumeration in the Java collections framework.

What are important methods in Iterator?

  1. hasNext() - Returns true if the iteration has more elements.
  2. next() -  Returns the next element in the iteration.
  3. remove() - Removes from the underlying collection the last element returned by the iterator (optional operation).

What are differences between Enumeration and Iterator?

An iterator over a collection. Iterator takes the place of Enumeration in the Java collections framework.
Iterators differ from enumerations in two ways:

                                                               i.      Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.

                                                             ii.      Method names have been improved.


In other words Iterator can remove an element whereas Enumeration can not.

Most Interesting methods?

  • hasNext() - Returns true if the iteration has more elements.
  • next() - Returns the next element in the iteration.
  • remove() - Removes from the underlying collection the last element returned by the iterator (optional operation).
<<Previous | Next>>
ċ
TestIterator.java
(1k)
Mahima Hardia,
Aug 19, 2011, 11:31 PM
Comments