- If we want to get objects one by one from the collection we should go for cursor.
- There are 3 type of cursors available in java
- Enumeration
- Iterator
- List Iterator
- Enumeration:-
- It is a cursor to retrieve objects one by one from the collection
- It is applicable for legacy classes
- We can create Enumeration object by using elements()
Public Enumeration elements(); Eg: Enumeration e = v.elements();
where v is a Vector Object
Enumeration Interface define the following two methods:
- Public Boolean has MoreElements();
- Public Object nextElement();
Example:-
import java.util.*; class Enumeration_Demo { public static void main(String[] args) { Vector v=new Vector(); for(int j=0;j<=10;j++) { v.addElement(j); } Enumeration e=v.elements(); System.out.println("Even numbers are :-"); while(e.hasMoreElements()) { Integer k=(Integer)e.nextElement(); if(k%2==0) System.out.println(k); } System.out.print("vector elements are :-" + v); } }
Output:-
Limitation of Enumeration are below:-
- Enumeration concept is applicable only for legacy classes & hence it is not universal cursor.
- By using Enumeration we van get only Read Access & we cant perform any remove operations.
- To over come these limitations sun people introduced Iterator in 1.2 verson.
2. Iterator:
- We can apply Iterator concept for any collection object. It is a universal cursor.
- While Iterating we can perform remove operation also in addition to read operation.
- We can use Iterator to get objects one by one from any collection object.
- We can get Iterator object by Iterator() of collection interface.
Iterator itr = C.iterator()
Iterator Interface defines the following 3 methods :
- public Boolean hasNext();
- public Object next();
- public void remove();
Example:-
import java.util.*; class Iterator_Demo { public static void main(String[] args) { ArrayList al=new ArrayList(); for(int j=0;j<=10;j++) { al.add(j); } System.out.println(al); Iterator itr=al.iterator(); while(itr.hasNext()) { Integer k=(Integer)itr.next(); if(k%2==0) System.out.println(k); else itr.remove(); } System.out.println(al); } }
Limitations of Iterator:
- Both enumeration and Iterator are single direction cursors only that’s why we can always move only forward direction and we can’t move to 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 list-iterator concept.
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 in addition to read and remove operations
- By using listIterator method we can create listIterator object.
ListIterator itr=l.listiterator();
- ListIterator interface supports following 9 methods
1) public boolean hasNext(); forward
2) public Object next(); forward
3) publicintnextIndex(); forward
4) publicbooleanhasPrevious(); backward
5) public Object previous(); backward
6) publicintpreviousIndex(); backward
7) public void remove();
8) public void set(Object o);
9) public void add(Object new);
Example:-
import java.util.*; class ListIterator_Demo { public static void main(String[] args) { LinkedList L=new LinkedList(); L.add("A"); L.add("B"); L.add("c"); L.add("D"); System.out.println(L); ListIterator ltr=L.listIterator(); while(ltr.hasNext()) { String s=(String)ltr.next(); if(s.equals("A")) { ltr.remove(); } } System.out.println(L); } }
Note:- listIterator is the most powerful cursor but it’s limitation is only applicable for “List objects”.