Three Cursors in Java Collections

If we want to get objects one by one from the collection we should go for the cursor. 

A Java Cursor is an Iterator used to iterate or traverse Collection Objects one by one. It can also be used to iterate or traverse Stream object’s elements in a sequential manner.

Table of Content :

 There are 3 types of cursors available in java 

 1). Enumeration 

 2). Iterator

 3). List Iterator

Enumeration

1). We can use Enumeration to get objects one by one from the legacy collection objects.

2). We can create an Enumeration object by using the elements() method.

public Enumeration elements();
Enumeration e=v.elements();

using Vector Object
Enumeration interface defines the following two methods

public boolean hasMoreElements();
public Object nextElement();
import java.util.*;
class EnumerationDemo { public static void main(String[] args) { Vector v=new Vector(); for(int i=0;i<=10;i++) { v.addElement(i); } System.out.println(v);//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Enumeration e=v.elements(); while(e.hasMoreElements()) { Integer i=(Integer)e.nextElement(); if(i%2==0) System.out.println(i);//0 2 4 6 8 10 } System.out.print(v);//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } }

Limitations of Enumeration

  • We can apply the Enumeration concept only for legacy classes and it is not a universal cursor.
  • By using Enumeration we can get only read access and we can't perform remove operations.
  • To overcome these limitations sun people introduced the Iterator concept in 1.2v.

Iterator

1). We can use an Iterator to get objects one by one from any collection object.

2). We can apply the Iterator concept to any collection object and it is a universal cursor.

3). While iterating the objects by Iterator we can perform both read and remove operations.

4). We can get the Iterator object by using the iterator() method of the Collection interface.

public Iterator iterator();
Iterator itr=c.iterator();

The iterator interface defines the following 3 methods.

public boolean hasNext();
public object next(); public void remove();
import java.util.*;
class IteratorDemo { public static void main(String[] args) { ArrayList a=new ArrayList(); for(int i=0;i<=10;i++) { a.add(i); } System.out.println(a);//[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Iterator itr=a.iterator(); while(itr.hasNext()) { Integer i=(Integer)itr.next(); if(i%2==0) System.out.println(i);//0, 2, 4, 6, 8, 10 else itr.remove(); } System.out.println(a);//[0, 2, 4, 6, 8, 10] } }

Limitations of Iterator

  • Both enumeration and Iterator are single-direction cursors only. That is we can always move only forward direction and we can't move in the backward direction.
  • While iterating by Iterator we can perform only read and remove operations and we can't perform replacement and addition of new objects.
  • To overcome these limitations sun people introduced listIterator concept.

ListIterator

1). ListIterator is the child interface of the Iterator.

2). By using listIterator we can move either to the forward direction (or) to the backward direction that is it is a bi-directional cursor.

3). While iterating by listIterator we can perform replacement and addition of new objects in addition to read and remove operations

4). By using listIterator method we can create listIterator object.

ListIterator interface defines the following 9 methods.

public ListIterator listIterator();
ListIterator itr=l.listIterator(); (l is any List object)
public boolean hasNext();
public Object next(); forward public int nextIndex(); public boolean hasPrevious(); public Object previous(); backward public int previousIndex(); public void remove(); public void set(Object new); public void add(Object new);
import java.util.*;
class ListIteratorDemo { public static void main(String[] args) { LinkedList l=new LinkedList(); l.add("Bala"); l.add("John"); l.add("Ketty"); l.add("Amenda"); System.out.println(l);//[Bala, John, Ketty, Amenda] ListIterator itr=l.listIterator(); while(itr.hasNext()) { String s=(String)itr.next(); if(s.equals("Datta")) { itr.remove(); } } System.out.println(l);//[Bala, Amol, Chandu] } }

The most powerful cursor is listIterator but its limitation is it is applicable only for "List objects".

Comparison between Enumeration Iterator and ListIterator

 Property Enumeration Iterator ListIterator
 1) Is it a legacy? Yes  no no
 2) It is applicable for?  Only legacy classes. Applicable for any collection object. Applicable for only list objects.
 3) Moment? Single direction cursor(forward) Single direction cursor(forward) Bi-directional.
 4) How to get it? By using elements() method.   By using iterator()method.  By using listIterator() method.
 5) Accessibility? Only read. Both read and remove. Read/remove/replace/add.
 6) Methods hasMoreElement()  nextElement() hasNext()  next()  remove()  9 methods

Questions related to Three Cursors in Java Collections

What is the difference between List and Set in Java?
Find Max and Min Elements of HashSet in Java
Iterable interface in java

That's it. We had seen the 3 cursors in the collection.

We can download complete code for this article over on GitHub.