Cursors in Collection framework
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.
public boolean hasMoreElements()
public Object nextElement()
Example:
Limitations of Enumeration
It is applicable only for legacy classes and it is not a universal cursor.
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
It is Introduced in 1.2 version.
We can get Iterator Object for any collection implemented class hence it is universal cursor.
while iterating objects, we are allowed perform removal operation in addition to read operation.
This interface contains the following 3 methods.
public boolean hasNext()
public Object next()
public void remove()
Example:
To over come these limitations we should go for ListIterator interface.
ListIterator
It has introduced in 1.2 version and it is child interface of Iterator.
It is a bi-directional cursor,i.e we can move either to the forward or backward direction.
While Iterating we can perform read,remove,replace and addition of new objects.
This interface defines the following 9 methods.
public boolean hasNext();
public boolean hasPrevious();
public Object next();
public Object previous();
public int nextIndex();
If there is no next element it returns size of the list.
public int previousIndex();
If there is no previous element it returns -1 .
public void remove();
public void set(Object new)
public void set(Object new)
public void add(Object new)
Example:
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).
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.
public boolean hasMoreElements()
public Object nextElement()
Example:
Limitations of Enumeration
It is applicable only for legacy classes and it is not a universal cursor.
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
It is Introduced in 1.2 version.
We can get Iterator Object for any collection implemented class hence it is universal cursor.
while iterating objects, we are allowed perform removal operation in addition to read operation.
This interface contains the following 3 methods.
public boolean hasNext()
public Object next()
public void remove()
Example:
Enumeration and Iterator are single directional cursors. They can always move towords forward direction only.
By using Iterator we can perform read and remove operations. And we can’t perform any replace or addition of new objectsTo over come these limitations we should go for ListIterator interface.
ListIterator
It has introduced in 1.2 version and it is child interface of Iterator.
It is a bi-directional cursor,i.e we can move either to the forward or backward direction.
While Iterating we can perform read,remove,replace and addition of new objects.
This interface defines the following 9 methods.
public boolean hasNext();
public boolean hasPrevious();
public Object next();
public Object previous();
public int nextIndex();
If there is no next element it returns size of the list.
public int previousIndex();
If there is no previous element it returns -1 .
public void remove();
public void set(Object new)
public void set(Object new)
public void add(Object new)
Example:
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).
No comments:
Post a Comment