Iterator and ListIterator in Java

Iterator and ListIterator are interfaces provided by Java API that are used to iterate over a Collection. They are both part of the Java Collection Framework. ListIterator is a sub-interface of Iterator and is specifically used to iterate of a List.

How to use an iterator?

Each of the collection implementation provides an implementation of an iterator that can be used to iterate over it. Below example show how to obtain an iterator from a collection object and use it to iterate over the collection.

Each collection object provide an iterator() method that can be used to obtain the iterator object. The hasNext() and next() method can be used to traverse over the elements of the collection as shown in the example below:

public class TestClass {

    public static void main(String[] args) {
    	Set<String> set = new HashSet<String>();
    	Iterator<String> iter = set.iterator();

Apart from the hasNext(), next() method it also contains a remove() method that can be used to remove an element from the collection.

Iterator vs ListIterator

1. Traversal Objects

Iterator can be used to iterate over any type of Collection like List, Set or a Map. Though we cannot directly iterate over a map implementation, we can however get the ketSet or the valueSet of the map and iterate over it.

ListIterator on the other hand can only be used to iterate over a List. It is a sub-interface of Iterator specifically meant to iterate over a List.

2. Traversal Direction

Iterator can be used to traverse only in forward direction.

ListIterator however can be used to traverse in both directions i.e. forward and backward. Apart from the hasNext() and next() methods ListIterator also has the hasPrevious() and previous() methods that can be used to traverse in the backward direction.

3. Modification while traversal

Iterator can only remove an element from the collection during traversal. Iterator has the remove() method which is used to remove an element from the collection at the current index during traversal.

Apart from removing an element, ListIterator also have the capability to add or replace an element during traversal. ListIterator provides the add() and set() methods to add and replace elements respectively.

4. Retrieve element index

Iterator does not provide any way to retrieve the index of the current element during traversal.

ListIterator provides the previousIndex() and nextIndex() methods that can be used to obtain the index of the element in the list while traversing.

Iterator vs Enumeration

Enumeration and Iterator are both used to traverse over the elements of a Collection, however there are some significant differences between the two.

Enumeration is a legacy class and is not supported by all the Collection object, it is only supported by the legacy collection classes like Vector and Hashtable. Iterator on the other hand is a standard class for traversing over a collection object and is supported by all collection implementation.

Enumeration does not support removal of elements during traversal, Iterator as we have already learnt supports removal of elements while traversal.

Iterators are more secure and safe as compared to Enumeration, since Iterator throws a ConcurrentModificationException in a multithreaded environment if a thread tried to modify a collection when another thread is iterating over it.

Below are the methods of the Enumeration

  • boolean hasMoreElements()
  • Object nextElement()

Internal Implementation of ArrayList

ArrayList is an implementation of List interface, used to store a list of objects. Unlike normal arrays, it is a dynamic data structure, its size dynamically increases when its capacity is exhausted.

View Article

Understanding equals() and hashCode() methods

The equals() and hashCode() are two primary methods of Java's Object Class which work together to uniquely identify any object in Java.

View Article

Internal Implementation of HashMap

HashMap is popular data structure in Java which stores data in key-value format. This article describes the internal implementation to store and retrieve data in a hashmap.

View Article

Internal Implementation of HashSet

HashSet is a member of the Java Collection Framework which is used to store unique entries. This article describes how a HashSet is internally implemented to store unique entries.

View Article

Identity HashMap in Java

IdentityHashMap in Java is a special implentation of the Map interface designed to be used in special cases where reference equality is required.

View Article

Introduction to Java Collections

The Java collections framework is a library that provides commonly reusable collection data structures. It consists of a set of various interfaces, implementations and algorithms used to represent...

View Article

Understanding TreeMap

A TreeMap is an implementation of the Map interface used to store key-value pair in sorted order of their keys. This articles describe how a Treemap is implemented in Java.

View Article