Loading

Cursors in Java collection

Key points:

  • In Java mainly two types of cursors: Iterator and List Iterator.
  • Both are pre-defined Interfaces present in java.util package.
  • Iterator and List Iterator are used to traverse a group of objects.
  • List Iterator is used to traverse objects either in a forward direction or in a backward direction. i.e. bi-directional cursor.

Iterator

Key Points:

  • The iterator can traverse the objects only in a forward direction hence it is called a 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 methods of 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 for Iterator:

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]

ListIterator

Key Points:

  • List Iterator can traverse the objects in both forward and reverse directions hence it is called a bi-directional 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 methods 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 for List Iterator:

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