Friday, 7 July 2023

Cursors in Java

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.
  1. Enumeration
  2. Iterator
  3. 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
  1. public boolean hasMoreElements();
  2. 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.
To Overcome Enumeration Limitations, we should go for Iterator.

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. 
         public Iterator iterator()
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

Servlet in AEM/Sling Servlet

  What is Servlet? A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between t...