Iterator interface

In this article, we will discuss Iterator interface in detail. This is introduced in Java 1.2 version and part of Collection framework

This is applicable for every collection classes

 

Iterator interface:

  • Iterator interface works on any collection classes
  • This is part of collection framework introduced in Java 1.2 version
  • Iterator interface allows to read objects one-by-one from collection object
  • Also, we can perform remove operation, which is not possible in the legacy Enumeration interface while iterating collection objects
  • Signature: public Iterator iterator(); of Collection interface
  • Present in java.util package

 

 

How to get Iterator object?

We can create Iterator object using iterator() method of Collection interface

For example,

Iterator itr = col.iterator(); // col is a any collection object

 

 

Limitation of Iterator interface:

  • Though Iterator interface is applicable for all collection classes but we can iterate over collection items only in FORWARD direction
  • Iterating over collection items from backward is not possible with Iterator interface
  • It is unidirectional or single directional cursor but NOT bi-directional cursor
  • Iterator interface does allows to perform both read & remove operations from iterating collection items overcoming the limitation of legacy Enumeration interface
  • But still doesn’t allow to perform addition of new object or replacing old object with new object, while iterating

 

 

Iterator interface methods:

 Iterator methods Description
boolean hasNext(); returns true, if there are more element\objects to be iterated

otherwise returns false, if iterator reaches end of Collection

Object next(); returns next element/object from Collection

throws NoSuchElementException, if there isn’t next element

void remove(); removes the current element

 

 

 

Iterator examples:

 

IteratorExample.java

package in.bench.resources.java.collection;

import java.util.ArrayList;
import java.util.Iterator;

public class IteratorExample {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		ArrayList<String> al = new ArrayList<String>();

		// adding elements to ArrayList object
		al.add("Sundar Pichai");
		al.add("Satya Nadella");  
		al.add("Shiv Nadar");  
		al.add("Shantanu Narayen");
		al.add("Francisco D’Souza");

		// creating Iterator reference
		Iterator<String> ceo = al.iterator();

		// enumerating using while loop
		while (ceo.hasNext()){
			System.out.println(ceo.next()); 
		}
	}
}

 

Output:

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Francisco D’Souza

 

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
https://docs.oracle.com/javase/7/docs/api/java/util/List.html
https://docs.oracle.com/javase/8/docs/api/java/util/List.html
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
https://docs.oracle.com/javase/7/docs/api/java/util/Vector.html
https://docs.oracle.com/javase/8/docs/api/java/util/Vector.html
https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html
https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html

 

 

Happy Coding !!
Happy Learning !!