Loading
Cursors in Java

Cursors in Java are used to traverse through collection of objects. Java provides two types of cursors.

1.  Iterator - Unidirectional cursor
2.  Listiterator - Bidirectional cursor

1. Iterator

A Iterator is a cursor taht allows traversing elements only in a forward direction.


Features

  • The iterator can traverse the objects only in a forward direction hence it is called as unidirectional cursor.
  • The iterator is used to traverse all types of collections. 
  • By using an Iterator cursor we can read and remove the objects.

Important Method in Iterator

1. hasNext() - Used to return true(returns Boolean result) if there exists an element in the collection.
2. next() - It is used to return the next element from the collection.
3. remove() - This method removes the element returned by next() from the collection. 

Example:

package com.quipoin;

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

public class Test {
	public static void main(String[] args) {
		ArrayList aList=new ArrayList<>();
		aList.add(10);
		aList.add("Praveen");
		aList.add(15.50);
		aList.add(21);

		Iterator i=aList.iterator();
		while(i.hasNext()) {
			System.out.println(i.next());
		}
		System.out.println("-------------------------------");
		System.out.println("List before remove:\n"+aList);
		System.out.println("-------------------------------");
		aList.remove(2);//Index 2 value is 12.20
		System.out.println("List after remove:\n"+aList);
	}
}


Output:

10
Praveen
15.5
21
-------------------------------
List before remove:
[10, Praveen, 15.5, 21]
-------------------------------
List after remove:
[10, Praveen, 21]

2. ListIterator

ListIterator is an advanced version of Iterator that allows both forward and backward traversal.


Features
  • List Iterator can traverse the objects in both forward and reverse directions hence it is called as bidirectional cursor.
  • List Iterator is used to traverse only the List type of collection. It cannot be used to traverse other than the List type of collection.
  • By using List Iterator we can read, remove, replace, and add the elements.

Important Method of List Iterator


1. hasNext() - returns true if there exists an element in the list.
2. next() - This method returns the next element present in the list.
3. previous() - returns the previous elements of the list.
4. hasPrevious() - returns true if there exists a previous element in the list.
5. remove() - This method removes the element returned by previous() from the collection.

Example:

package com.quipoin;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class DemoListIterator {
	public static void main(String[] args) {
		LinkedList list=new LinkedList<>();

		list.add("Java");
		list.add("Sql");
		list.add("Spring_Boot");
		list.add("Python");

		System.out.println("Traversing elements in forward direction!!");
		ListIterator itr=list.listIterator();
		while(itr.hasNext()) {
			System.out.println(itr.next());
		}
		System.out.println("------------------------------------------");
		System.out.println("Traversing elements in reverse direction!!");
		while(itr.hasPrevious()) {
			System.out.println(itr.previous());
		}
	}
}


Output:

Traversing elements in forward direction!!
Java
Sql
Spring_Boot
Python
------------------------------------------
Traversing elements in reverse direction!!
Python
Spring_Boot
Sql
Java


Difference Between Iterator and List Iterator

FeatureIteratorList Iterator
Traversal DirectionForward onlyBoth forward and backward
Applicable to All CollectionsOnly List (Eg. - ArrayList, LinkedList, etc.)
Add ElementsNot PossiblePossible
Modify ElementsNot PossiblePossible
Remove ElementsPossiblePossible

Two Minute Drill

Iterator

  • Works with all Java collections (List, Set, Queue, etc.).
  • Traverses elements only in the forward direction.
  • Can remove elements but cannot modify or add them.

List Iterator

  • Works only with List implementations (ArrayList, LinkedList).
  • Supports both forward and backward traversal.
  • Allows adding, modifying, and removing elements while iterating.