What is Cursor?
- If We want to get Objects One by One from the Collection, than we should go for Cursor.
- 3 Types of Cursor Available in Java.
- Enumeration
- Iterator
- ListIterator
Enumeration
- We can Use Enumeration to get Objects One By One From Legacy Classes.
- We can create Enumeration Object By Using Elements Method of Vector Classes.
public Enumeration elements();
for eg:
Enumeration e = v.elements();
Methods
- public boolean hasMoreElements();
- public object nextElement();
Limitations of Enumeration(1.0v)
- We can apply Enumeration concept only for Legacy Classes and it is not Universal Cursor.
- By using Enumeration we can get only Read Access & We can't perform Remove Operation.
Iterator(Interface)
- We can apply Iterator Concept for any Collection Object.
- It is Universal Cursor.
- By using, Iterator We can Perform Both Read & Remove Operations.
- We can create Iterator Object by using, Iterator Method of Collection Interface.
for eg:
Iterator itr = c iterator();
Here, c is Any Collection Object.
Methods of Iterator
- public boolean hasNext();
- public Object next();
- public void remove();
Limitations of Iterator
- By using, Enumeration & Iterator we can always move only towards Forward Direction & we can't move towards Backward direction these are Single direction cursor.
- By Using, Iterator we can perform only Read & Remove Operations we can't perform Replacement & Addition of new Object.
To Overcome Iterator Limitations we should go for ListIterator.
ListIterator(Interface)
- By Using, ListIterator we can move either to the Forward & Backward Direction.
- It is Bidirectional Cursor.
- By using ListIterator we can perform, Replacement an addition of new Object in addition to Read & Remove Operations.
- We can create ListIterator By using, ListIterator method for ListIterator Interface.
public ListIterator listIterator();
for eg:
ListIterator itr = l.listIterator();
Here, l is Any List Object.
Methods Of ListIterator
- ListIterator is Child Interface of Iterator Hence, All methods present in Iterator By default, available to the ListInterface.
Forward Direction Method
- public boolean hasNext()
- public Object next()
- public int nextIndex()
Backward Direction Method
- public boolean previousNext()
- public Object previous()
- public int previousIndex()
Extra Method
- public void move()
- public void add(object o)
- public void set(object o)
Limitations of ListIterator
- The Most Powerful Cursor is ListIterator But, it is applicable only for List Objects.
No comments:
Post a Comment