Cursor in Java

Last Updated : 27 Oct, 2025

A Java Cursor is an object that is used to iterate, traverse, or retrieve a Collection or Stream object’s elements one by one.

  • Cursors allow sequential access to each element in a collection.
  • Some cursors (like ListIterator) allow addition, removal, or replacement of elements during traversal.
  • Cursors are generic, ensuring type safety and avoiding ClassCastException

Types of cursor in java

Java provides three types of cursors, depending on the collection type and the operations supported.

Types-of-cursor
java-cursor

1. Enumeration Cursor

The Enumeration cursor is a legacy cursor, introduced before Java 2 (JDK 1.2). It is used mainly with older classes like Vector and Hashtable.

Declaration of Enumeration

public interface Enumeration<E>

Important Methods of Enumeration

  • boolean hasMoreElements(): Returns true if more elements are available.
  • E nextElement(): Returns the next element of the collection.
Java
import java.util.*;

public class GFG{
    
    public static void main(String[] args){
        
        Vector<Integer> v = new Vector<>();
        v.add(10);
        v.add(20);
        v.add(30);

        Enumeration<Integer> e = v.elements();

        System.out.println("Elements using Enumeration:");
        while (e.hasMoreElements()) {
            System.out.println(e.nextElement());
        }
    }
}

Output
Elements using Enumeration:
10
20
30

Note: Enumeration is read-only and works only in forward direction.

2. Iterator Cursor

The Iterator cursor is the universal cursor introduced in JDK 1.2. It can be used with all classes of the Java Collection Framework such as ArrayList, HashSet, LinkedList, etc.

Declaration of Iterator

public interface Iterator<E>

Important Methods of Iterator

  • boolean hasNext(): Returns true if more elements exist.
  • E next(): Returns the next element.
  • void remove(): Removes the current element (optional operation).
Java
import java.util.*;

public class GFG{
    
    public static void main(String[] args){
        
        Collection<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");

        Iterator<String> itr = names.iterator();

        System.out.println("Elements using Iterator:");
        while (itr.hasNext()) {
            String name = itr.next();
            System.out.println(name);
            if (name.equals("Bob")){
            
            // safe removal
                itr.remove();  
            }
        }

        System.out.println("After removal: " + names);
    }
}

Output
Elements using Iterator:
Alice
Bob
Charlie
After removal: [Alice, Charlie]

Note: Using Iterator traverse only in forward direction and cannot modify or add elements.

3. ListIterator Cursor

The ListIterator cursor is a bidirectional cursor, introduced in JDK 1.2. It is used only with List implementations such as ArrayList and LinkedList.

Declaration of ListIterator

public interface ListIterator<E> extends Iterator<E>

Important Methods of ListIterator

  • boolean hasNext(): Checks if next element exists.
  • E next(): Returns the next element.
  • boolean hasPrevious(): Checks if previous element exists.
  • E previous(): Returns the previous element.
  • void remove(): Removes current element.
  • void add(E e): Inserts new element.
  • void set(E e): Replaces the last element returned by next() or previous().
Java
import java.util.*;

public class GFG{
    
    public static void main(String[] args){
        
        List<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Python");
        list.add("C++");

        ListIterator<String> li = list.listIterator();

        System.out.println("Forward Traversal:");
        while (li.hasNext()) {
            System.out.println(li.next());
        }

        System.out.println("Backward Traversal:");
        while (li.hasPrevious()) {
            System.out.println(li.previous());
        }

        // Modify elements
        li = list.listIterator();
        while (li.hasNext()) {
            String lang = li.next();
            if (lang.equals("Python")) {
                li.set("Kotlin");  // Replace
            }
        }

        System.out.println("Modified List: " + list);
    }
}

Output
Forward Traversal:
Java
Python
C++
Backward Traversal:
C++
Python
Java
Modified List: [Java, Kotlin, C++]

Difference Between Enumeration, Iterator, and ListIterator

FeatureEnumerationIteratorListIterator
Applicable CollectionsWorks only with legacy classes like Vector, Stack, and Hashtable.Can traverse any type of collection.Can traverse only List classes such as ArrayList, LinkedList, etc.
Traversal DirectionOnly forward direction.Only forward direction.Both forward and backward directions.
Object CreationCreated using elements() method of legacy classes.Created using iterator() method of the Collection interface.Created using listIterator() method of the List interface.
Element DeletionNot allowed.Allowed using remove() method.Allowed using remove() method.
Element AdditionNot allowed.Not allowed (throws ConcurrentModificationException if tried).Allowed using add() method.
Element ModificationNot allowed.Not allowed.Allowed using set() method.
Index AccessNot supported.Not supported.Supported using nextIndex() and previousIndex() methods.
Traversal ScopeLegacy collections only.All collections.List collections only.


Comment
Article Tags: