Search This Blog

Iterator in Java

What is Iterator?

Java provides Collection framework to handle different type of objects in different ways eg. List,Set,Map etc. Now in order to use these collections efficiently we need to traverse through them to read\write\delete the elements of collections.
Iterator is a java interface which helps us in traversing any collection.

Iterator is a java interface which replaced Enumeration.It belongs to collection framework in Java. List,Set provide iterator to traverse through them whereas Map implementation doesn't provide iterator which could traverse through them but we can use map iterator in some different way to get the keyset() out of map.Further in this tutorial we will see an example of map iterator .
Using iterator we can iterate in one direction only(except for ListIterator) and once finished iterating the collection completely we need to define another iterator in case of iterating the collection again.
Any class that extends Iterable class can return iterator object which can be used for traversing the class.

Structure of Iterator interface : 

public interface Iterator<E> {
   
    boolean hasNext();
    
    E next();

    void remove();
}
hasNext() : Returns true if iterator has more elements to iterate else returns false.

Next() : Returns next element in iteration provided hasNext() returns true.
remove() : it removes the last element returned by next() from the collection. This method could be called once for each call for next() method. If the collection is modified while iteration is in process in anyway other than by calling this method, iteration could behave in unjustified manner and could throw one of the below exception

  • ConcurrentModificationException
  • IllegalStateException
Creating Iterator in Java
As we know that any class which extends Iterable class can provide iterator object ,Collection class extends Iterable  and the interfaces like List, Set extends Collection hence the implementation classes of these interfaces can provide iterator object eg. ArrayList , LinkedList etc.
To create iterator we should follow below mentioned steps:
  • create iterator object by calling iterator () method on collection object.
  • You can call hasNext() on iterator object to find if there is any element in iterator object.
  • next() mehod call on iterator object will give you the next element of iterator.
Iterator by Example

package com.varun.examples;

import java.util.*;

public class IteratorTest {

 public static void main(String[] args) {
  List myList = new ArrayList();
  myList.add("dog");
  myList.add("elephant");
  myList.add("fox");
  myList.add("goat");

  Iterator itr = myList.iterator();
  while (itr.hasNext()) {
   String val = itr.next();
   System.out.println(val);
   itr.remove();
  }

  System.out.println("List Iterator ::" + myList.toString());

  Set st = new HashSet();
  st.add("dog : Set");
  st.add("elephant : Set");
  st.add("fox : Set");
  st.add("goat : Set");

  Iterator itrSet = st.iterator();
  while (itrSet.hasNext()) {
   System.out.println(itrSet.next());
   itrSet.remove();
  }

  System.out.println("Set iterator::" + st.toString());

 }

}

No comments:

Post a Comment