Iterator Interface in Java

Prev Tutorial Next Tutorial

Iterator Interface in Java

It is one of the predefined interface present in java.util.* package. The purpose of this interface is that to extract or retrieve the elements of collection variable only in forward direction but not in backward direction. By default an object of iterator is pointing just before the first element of any collection framework variable.

Points to Remember

  • Iterator allows to remove the elements from collection object.
  • Iterator is not Synchronized.
  • Using Iterator elements of collection can be access only in forward direction.
  • Iterator can be available to all the collection classes.
  • Every collection class contains Iterator() and that returns Iterator object, using this object reference elements can be retrieved from collection.

Methods of Iterator Interface

  • public boolean hasNext()
  • public object next()
  • remove()

public boolean hasNext()

This method return true provided by Iterator interface object is having next element otherwise it returns false.

public object next()

This method is used for retrieving next element of any collection framework variable provided public boolean hasNext(). If next elements are available then this method returns true other wise it return false.

public object remove()

Remove from collection the last element returned by Iterator.

Example of Iterator

import java.util.*;

class IteratorDemo
public static void main(String args[])

ArrayList<Integer> al=new ArrayList<Integer>();  // creating arraylist

Iterator itr=al.iterator();  // getting Iterator from arraylist to traverse elements  



Prev Tutorial Next Tutorial

Download Projects

Google Adsense Advertisements
Free Classified Site – Bedpage

Yahoo Advertisements