Java Iterator – Iterator in Java
In this post we are going to discuss about some basics of Java Enumeration and in-depth discussion about Java Iterator. As Java Enumeration interface is deprecated, it is not recommended to use this in our applications.
Tech Jitendra Provides,
In this post we are going to this discuss the following concepts about Java’s Iterator.
Java Four Cursors
First of all, I would like to define what is a Java Cursor? A Java Cursor is an Iterator, which is used to iterate or traverse or retrieve a Collection or Stream object’s elements one by one.
Java supports the following four different cursors.
- Enumeration
- Iterator
- ListIterator
- Spliterator

Each Java cursor have some advantages and drawbacks. We will discuss some basics about Enumeration and full details about Iterator in this posts. We will discuss about ListIterator and Spliterator in my coming posts.
Java Enumeration Limitations
Java Enumeration is available since Java 1.0 and it has many limitations and not advisable to use in new projects.
- It is available since Java 1.0 and legacy interface.
- It is useful only for Collection Legacy classes.
- Compare to other Cursors, it has very lengthy method names: hasMoreElements() and nextElement().
- In CRUD Operations, it supports only READ operation. Does not support CREATE, UPDATE and DELETE operations.
- It supports only Forward Direction iteration. That’s why it is also know as Uni-Directional Cursor.
- It is not recommended to use it in new code base or projects.
NOTE:- What is CRUD operations in Collection API?
- CREATE: Adding new elements to Collection object.
- READ: Retrieving elements from Collection object.
- UPDATE: Updating or setting existing elements in Collection object.
- DELETE: Removing elements from Collection object.
To overcome all these issues, Java come-up with new Cursors: Iterator and ListIterator in Java 1.2. It has introduced a new type of Cursor: Spliterator in Java 1.8.
We will discuss about Iterator with some suitable examples in this post.
Java Iterator
In Java, Iterator is an interface available in Collection framework in java.util package. It is a Java Cursor used to iterate a collection of objects.
- It is used to traverse a collection object elements one by one.
- It is available since Java 1.2 Collection Framework.
- It is applicable for all Collection classes. So it is also known as Universal Java Cursor.
- It supports both READ and REMOVE Operations.
- Compare to Enumeration interface, Iterator method names are simple and easy to use.
Java Iterator Class Diagram
As shown in the Class Diagram below, Java Iterator has four methods. We are already familiar with first four methods. Oracle Corp has added fourth method to this interface in Java SE 8 release.

Java Iterator Methods
In this section, we will discuss about Java Iterator methods in-brief. We will explore these methods in-depth with some useful examples in the coming section.
- boolean hasNext():Returns true if the iteration has more elements.
- E next(): Returns the next element in the iteration.
- default void remove(): Removes from the underlying collection the last element returned by this iterator.
- default void forEachRemaining(Consumer action): Performs the given action for each remaining element until all elements have been processed or the action throws an exception.
Java Iterator Basic Example
First discuss about how to get an Iterator object from a Collection. Each and every Collection class has the following iterator() method to iterate it’s elements.
Iterator<E> iterator()
It returns an iterator over the elements available in the given Collection object.
Example-1:-
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class ExternalIteratorDemo
{
public static void main(String[] args) {
List<String> names = newLinkedList<>();
names.add("Rams");
names.add("Posa");
names.add("Chinni");
// Getting Iterator
Iterator<String> namesIterator = names.iterator();
// Traversing elements
while(namesIterator.hasNext()){
System.out.println(namesIterator.next());
}
}
}
Example-2:-
import java.util.LinkedList;
import java.util.List;
public class InternalIteratorDemo
{
public static void main(String[] args) {
List<String> names = newLinkedList<>();
names.add("Rams");
names.add("Posa");
names.add("Chinni");
for(String name: names){
System.out.println(name);
}
}
}
If we observe the two above examples, both examples are doing the same thing. In Example-1, we have created Iterator object externally and retrieved List object elements one by one. In Example-2, we have Not created Iterator object externally. We are using Enhanced for loop to retrieve the List object elements one by one.
Enhanced for loop uses Iterator object internally and do the same thing like External Iterator example. So both examples gives the same output as shown below.
Output:-
Rams
Posa
Chinni
Develop Custom Class Iterator
In previous section, we have discussed about how Collection API has implemented the iterator() method to iterate it’s elements with or without using Enhanced For Loop.
In this section, we will discuss about how to provide similar kind of functionality for a User-Defined or Custom classes. We should follow these instructions to provide this functionality:
- Define Custom class.
- Define Collection class to this Custom class.
- This Collection class should implement Iterable interface with Custom class as Type parameter.
- This Collection class should provide implementation of Iterable interface method: iterator().
If we implement these instructions to our Custom class, then it’s ready to use Enhanced For Loop or external Iterator object to iterate it’s elements.
Let us develop a simple example to understand these instructions clearly.
Example:-
public class Employee {
private int empid;
private String ename;
private String designation;
private double salary;
public Employee(int empid,String ename,String designation,doublesalary){
this.empid = empid;
this.ename = ename;
this.designation = designation;
this.salary = salary;
}
public int getEmpid() {
return empid;
}
public String getEname() {
return ename;
}
public String getDesignation() {
return designation;
}
public double getSalary() {
return salary;
}
@Override
public String toString(){
return empid + "\t" + ename + "\t"+ designation + "\t" + salary;
}
}
import java.util.*;
public class Employees implementsIterable{
private List<Employee> emps = null;
public Employees(){
emps = new ArrayList<&glt;();
emps.add(newEmployee(1001,"Rams","Lead", 250000L));
emps.add(newEmployee(1002,"Posa","Dev", 150000L));
emps.add(newEmployee(1003,"Chinni","QA", 150000L));
}
@Override
public Iterator<Employee> iterator() {
return emps.iterator();
}
}
public class EmployeesTester {
public static void main(String[] args) {
Employees emps = new Employees();
for(Employee emp : emps){
System.out.println(emp);
}
}
}
Output:-
1001 Rams Lead 250000.0
1002 Posa Dev 150000.0
1003 Chinni QA 150000.0
Thinking you
Tech Jitendra
