To retrieve objects one by one from Collection we have to use cursors.

There are 3 types of cursors available

1. Enumeration
2. Iterator
3. ListIterator

Enumeration

This interface has introduced in 1.0 version it contains the following 2 methods.

  1. public boolean hasMoreElements()
  2. public Object nextElement()

Example:

import java.util.Enumeration;
import java.util.Vector;

public class EnumerationDemo {

	public static void main(String[] args) {
		Vector vector = new Vector();
		for (int item = 1; item <= 5; item++) {
			vector.addElement(item);
		}
		System.out.println(vector);
		Enumeration enumeration = vector.elements();
		while (enumeration.hasMoreElements()) {
			Integer integer = (Integer) enumeration.nextElement();
			System.out.println(integer);
		}
	}
}
/*
Output:
[1, 2, 3, 4, 5]
1
2
3
4
5
*/

Limitations of Enumeration

  1. It is applicable only for legacy classes and it is not a universal cursor.
  2. While iterating the objects by using enumeration we can get only read access and we can’t perform removal or replacement operations.

To overcome these problems we should go for Iterator interface.

Iterator

  1. It is Introduced in 1.2 version.
  2. We can get Iterator Object for any collection implemented class hence it is universal cursor.
  3. while iterating objects, we are allowed perform removal operation in addition to read operation.

This interface contains the following 3 methods.

  1. public boolean hasNext()
  2. public Object next()
  3. public void remove()

Example:

import java.util.ArrayList;
import java.util.Iterator;
class IteratorDemo {
	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]
*/

  1. Enumeration and Iterator are single directional cursors. They can always move towords forward direction only.
  2. By using Iterator we can perform read and remove operations. And we can’t perform any replace or addition of new objects

To over come these limitations we should go for ListIterator interface.

ListIterator

  1. It has introduced in 1.2 version and it is child interface of Iterator.
  2. It is a bi-directional cursor,i.e we can move either to the forward or backward direction.
  3. While Iterating we can perform read,remove,replace and addition of new objects.

This interface defines the following 9 methods.

  1. public boolean hasNext();
  2. public boolean hasPrevious();
  3. public Object next();
  4. public Object previous();
  5. public int nextIndex();
    If there is no next element it returns size of the list.
  6. public int previousIndex();
    If there is no previous element it returns -1 .
  7. public void remove();
  8. public void set(Object new)
  9. public void set(Object new)
  10. public void add(Object new)

Example:

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

class ListIteratorDemo {
	public static void main(String arg[]) {
		LinkedList l = new LinkedList();
		l.add("C");
		l.add("C++");
		l.add("JAVA");
		l.add("PHP");
		System.out.println(l);
		ListIterator ltr = l.listIterator();
		while (ltr.hasNext()) {
			String s = (String) ltr.next();
			if (s.equals("C++")) {
				ltr.add("C plus plus");
			}
		}
		System.out.println(l);
	}
}
/*
Output:
[C, C++, JAVA, PHP]
[C, C++, C plus plus, JAVA, PHP]
*/

Note:The most powerful cursor is listIterator. But it’s main limitation is it is applicable only for list implemented classes (ArrayList, LinkedList, Vector, Stack).