Cursors of Collection Framework in Java

Cursors of Collection Framework in Java

In this article, I am going to discuss Cursors of Collection Framework in Java with Examples. Please read our previous article where we discussed List Collections in Java. As part of this article, we are going to discuss the following pointers in detail.

  1. Cursors of Collection Framework in Java
  2. Iterator Cursors of Collection Framework in Java
  3. ListIterator Cursors of Collection Framework
  4. Enumeration Cursors in Java
  5. Comparison Between Iterator, ListIterator and Enumeration Cursors in Java
Cursors of Collection Framework in Java:

Cursors are mainly used to access the elements of any collection. We have the following three types of cursors in Collection Framework:

  1. Iterator
  2. ListIterator
  3. Enumeration
Iterator Cursors of Collection Framework in Java

This cursor is used to access the elements in the forward direction only. This cursor can be applied to any Collection Interfaces. While accessing the methods we can also delete the elements. Iterator is an interface and we cannot create an object directly. If we want to create an object for Iterator we have to use iterator() method.

Creation of Iterator
Syntax : Iterator it = c.iterator();
Here, iterator() method internally creates and returns an object of a class which implements Iterator Interface.

Methods of Iterator:
  1. boolean hasNext(): returns true if there is a next element in the array list.
  2. Object next(): returns the next element in the array list.
  3. void remove(): removes an element from an array list.
Sample Program to demonstrate Iterator Cursors in Java
import java.util.*;
public class IteratorDemo
{
    public static void main (String[]args)
    {
        LinkedList < Integer > ll = new LinkedList < Integer > ();
        ll.add (10);
        ll.add (25);
        ll.add (50);
        ll.add (20);
        ll.add (25);
        ll.add (23);
        ll.add (60);
        ll.add (25);
        ll.add (30);
        ll.add (40);
        ll.add (15);
        ll.add (25);
        System.out.println (ll);
        Iterator it = ll.descendingIterator ();
        while (it.hasNext ())
        {
         System.out.println (it.next ());
        }
        Iterator it1 = ll.descendingIterator ();
        while (it1.hasNext ())
        {
         Integer e = (Integer) it1.next ();	//down casting
         if (e == 25)
         {
             it1.remove ();
         }
        }
        System.out.println (ll);
    }
}

Output:

Program to demonstrate Iterator Cursors in Java

ListIterator Cursors of Collection Framework in Java

This cursor is used to access the elements of Collection in both forward and backward directions. This cursor can be applied only for List category Collections. While accessing the methods we can also add, set, delete elements. ListIterator is an interface and we cannot create Object directly. If we want to create an object for ListIterator we have to use ListIterator() method.

Creation of ListIterator
Syntax : ListIterator it = l.listIterator();
Here, listIterator() method internally creates and returns an object of a class which implements ListIterator Interface.

Methods of ListIterator:
  1. boolean hasNext(): return true if the given list iterator contains more number of elements during traversing the given list in the forward direction.
  2. Object next(): return the next element in the given list. This method is used to iterate through the list.
  3. boolean hasPrevious():  is used to retrieve and remove the head of the deque.
  4. Object previous(): return the previous element from the list and moves the cursor in a backward position. The above method can be used to iterate the list in a backward direction.
  5. int nextIndex(): return the index of the element which is returned by the next() method.
  6. int previousIndex(): return the index of the given element which is returned by a call to previous. The method may return -1 if and only if the iterator is placed at the beginning of the list.
  7. void remove():  remove the last element from the list which is returned by next() or previous() method.
  8. void set(Object obj): is used to replace the last element which is returned by the next() or previous() along with the given element.
  9. void add(Object obj): is used to insert the given element into the specified list. The element is inserted automatically before the next element may return by the next() method.
Sample Program to demonstrate ListIterator Cursors of Collection Framework in Java
import java.util.LinkedList;
import java.util.ListIterator;

public class ListIteratorDemo
{
    public static void main (String[]args)
    {
        LinkedList < Integer > ll = new LinkedList < Integer > ();
        ll.add (10);
        ll.add (25);
        ll.add (50);
        ll.add (20);
        ll.add (25);
        ll.add (23);
        ll.add (40);
        ll.add (15);
        ll.add (25);
        System.out.println (ll);
        ListIterator < Integer > lit = ll.listIterator ();
        
        System.out.println ("Elements in Forward Direction");
        while (lit.hasNext ())
        {
         System.out.println (lit.next () + " ");
        }
        
        System.out.println ("\n elements in Backward direction");
        while (lit.hasPrevious ())
        {
         System.out.println (lit.previous () + " ");
        }
        while (lit.hasNext ())
        {
         Object o = lit.next ();
         Integer e = (Integer) o;
         if (e == 23)
         {
             lit.add (56);
         }
         if (e == 15)
         {
             lit.set (15000);
         }
         if (e == 25)
         {
             lit.remove ();
         }
        }
        System.out.println ("\n New List:" + ll);
    }
}

Output:

Program to demonstrate ListIterator Cursors of Collection Framework in Java

Enumeration Cursors of Collection Framework in Java

This cursor is used to access the elements of Collection only in a forward direction. This is a legacy cursor and can be applied only for legacy classes like Vector, Stack, Hashtable. Enumeration is also an interface and we cannot create the object directly. If we want to create an object for Enumeration we have to use a legacy method called elements() method. The Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.

Creation of Enumeration
Syntax : Enumeration e = v.elements();
Here, elements() method internally creates and returns an object of a class which implements Enumeration Interface.

Methods of Enumeration
  1. boolean hasMoreElements(): When implemented, it must return true while there are still more elements to extract, and false when all the elements have been enumerated.
  2. Object nextElement(): This returns the next object in the enumeration as a generic Object reference.
Program to demonstrate Enumeration Cursors of Collection Framework in Java
import java.util.*;
public class EnumerationDemo {
 public static void main(String[] args) {
  Vector<Integer> v = new Vector<Integer>();
  v.add(10);
  v.add(25);
  v.add(50);
  v.add(20);
  v.add(25);
  v.add(23);
  v.add(25);
  System.out.println(v);
  Enumeration e = v.elements();
  while(e.hasMoreElements()) {
   System.out.println(e.nextElement()+" ");
  }
 }
}

Output:

Program to demonstrate Enumeration Cursors of Collection Framework in Java

Comparison Between Iterator, ListIterator and Enumeration Cursors in Java

Comparison Between Iterator, ListIterator and Enumeration Cursors in Java

In the next article, I am going to discuss Set Collections in Java with examples. Here, in this article, I try to explain Cursors of Collection Framework in Java and I hope you enjoy this Cursors of Collection Framework in Java article.

Leave a Reply

Your email address will not be published. Required fields are marked *