Cursors in Collections

Cursors in Collections

In this tutorial, we are going to discuss cursors in collections. A Java Cursor is an Iterator used to iterate or traverse or retrieve a Collection or Stream object’s elements one by one.

Cursors are not a concept directly associated with collections in Java, as they are in some other programming languages or database systems. In Java, collections are typically traversed using iterators or enhanced for loops, rather than cursors.

From the collection object to retrieve object we can use the following 3 cursors.

  1. Enumeration
  2. Iterator
  3. ListIterator
Cursors in Collections
1. Enumeration
  • This interface has introduced in 1.0 version it contains the following 2 methods.
public boolean hasMoreElements();
public Object nextElement(); 
  • We can use Enumeration to get objects one by one from the legacy collection objects.
  • We can create Enumeration object by using elements() method.
public Enumeration elements(); 
Enumeration e=v.elements(); //using Vector Object
package com.ashok.collections;

import java.util.*;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyEnumaretion {
   public static void main(String arg[]) {
      Vector vector = new Vector();
      for (int i = 0; i <= 10; i++) {
         vector.addElement(i);
      }
      System.out.println(vector);
      Enumeration e = vector.elements();
      while (e.hasMoreElements()) {
         Integer i = (Integer) e.nextElement();
         if ((i % 2) == 0)
            System.out.println(i);
      }
      System.out.println(vector);
   }
}

Output

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
0
2
4
6
8
10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Limitations of Enumeration
  1. It is applicable only for legacy classes and it is not a universal cursor.
  2. While iterating the elements by using enumeration we can perform only read operation and we can’t perform any modify/removal operations.

To overcome these problems we should go for Iterator interface.

2. Iterator
  • Introduced in 1.2 version.
  • We can use Iterator to get objects one by one from any collection object.
  • We can apply the Iterator concept for any collection object, and it is a universal cursor.
  • While iterating the objects by Iterator, we can perform both read and remove operations.

This interface contains the following 3 methods.

boolean hasNext();
Object next();
void remove();
package com.ashok.collections;

import java.util.*;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyIterator {
   public static void main(String arg[]) {
      ArrayList  arrayList = new ArrayList();
      for (int i = 0; i <= 10; i++) {
         arrayList.add(i);
      }
      System.out.println(arrayList);
      Iterator itr = arrayList.iterator();
      while (itr.hasNext()) {
         Integer i = (Integer) itr.next();
         if ((i % 2) == 0) {
            System.out.println(i);
         } else {
            itr.remove();
         }
      }
      System.out.println(arrayList);
   }
}

Output

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
0
2
4
6
8
10
[0, 2, 4, 6, 8, 10]
3. ListIterator
  • ListIterator is the child interface of Iterator.
  • By using ListIterator we can move either to the forward direction (or) to the backward direction. That is, it is a bi-directional cursor.
  • While iterating by ListIterator, we can perform replacement and addition of new objects to read and remove operations.
  • By using the ListIterator method, we can create the ListIterator object.
public ListIterator listIterator(); 
ListIterator itr=list.listIterator(); // Here list is any List object 

ListIterator interface defines the following 9 methods.

1. public boolean hasNext();
2. public Object next(); forward
3. public int nextIndex();
4. public boolean hasPrevious();
5. public Object previous(); backward
6. public int previousIndex();
7. public void remove();
8. public void set(Object new);
9. public void add(Object new); 
package com.ashok.collections;

import java.util.LinkedList;
import java.util.ListIterator;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class MyListIterator {
   public static void main(String[] args) {
      LinkedList list = new LinkedList();
      list.add("Ashok");
      list.add("Vinod");
      list.add("Dillesh");
      list.add("Thiru");
      System.out.println(list);
      ListIterator itr = list.listIterator();
      while (itr.hasNext()) {
         String s = (String) itr.next();
         if (s.equals("Vinod")) {
            itr.remove();
         }
      }
      System.out.println(list);
   }
}

Output

[Ashok, Vinod, Dillesh, Thiru]
[Ashok, Dillesh, Thiru]

That’s all about the Cursors in Collections in java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

Cursors in Collections
Scroll to top