Iterator in java

Iterator is a interface in java, help you to traverse the element of collection.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Iterator in java

In this section you will learn about Iterator in java.  In order to cycle through the element of collection, iterator is used. For example, iterator can be used to display the element of collection. Iterator enables removing or fetching element from the collection. Iterator replace enumeration in java collection.

Iterator in java, is traversing the object from collection. As we know, for loop, for-each loop and while loop, they all are traversed based on index but Iterator in java is to traverse through object as well as accessing data from collection. Java Iterator is an interface that belongs to collection and allows iterate through the element of collection. It comes under java.util package as a public interface Iterator. It has three methods, they are as follows:

  • boolean hasNext() :  Returns true if the iteration has more element.
  • Object next(): Returns the next element in the collection.
  • void remove(): Removes from the underlying collection the last element is returned by the iterator.

How to create Iterator in Java?

Every class of collection has iterator() method in java that return an iterator to the starting of collection. Using iterator object you can access the element of collection, one element at a time. To use an itearator for retrieving the data from collection, follow these steps:

  • Call  collection iterator(), iterator will be obtained at the start of collection.
  • Apply a loop that make a call to hasNext() method. as long as hasNext() return true.
  • Retrieve each element by calling next(), within the loop.

Example :  Code using iterator to display the content of queue

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
public class IteratorDemo {
public static void main(String args[])
	{
		        Queue qe=new LinkedList();
                        qe.add("Welcome");
		        qe.add("To");
		        qe.add("Rose");
		        qe.add("India");
		        Iterator it=qe.iterator();

		        System.out.println("Initial Capacity of Queue :"+qe.size());

		        while(it.hasNext())
		        {
		       System.out.println( it.next());    
		        }
		      }
           }

In the above code, a queue is created by using iterator object and each element of queue is displayed.

Output : After compiling and executing the above code, output will be as follows.

Advertisements

Ads
Share on Google+Share on Google+

Iterator in java

Posted on: June 15, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Iterator in java  

Post your Comment


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

Ads

 

Ads