Wednesday, 3 January 2018

Cursors In Java

Cursors In Java, Enumeration,Iterator,ListIterator,Enumeration method,iterator method, ListIterator method in java,Limitations of iterator ,Enumeration method in java,iterator method in java, ListIterator method,Enumeration,Comparison of 3 Cursors,Enumeration,Comparison of 3 Cursors in java,Enumeration in java,


  •     If we want to get Objects One by One from the Collection  then  we should go for Cursors.
  •    There are 3 Types of Cursors Available in Java.

  1.               Enumeration 
  2.               Iterator
  3.               ListIterator 
 Enumeration:

· We can Use Enumeration to get Objects One by One from the Collection.
· We can Create Enumeration Object by using elements(). 

        public Enumeration elements();

Eg:-
        Enumeration e = v.elements(); //v is Vector Object.

Methods:
        ·       public booleanhasMoreElements();
        ·       public Object nextElement();

vector v = new vector();
      for(int i=0; i<=10;i++)
      {
            v.addElement(i);
      }
      System.out.println(v); //[0,1,2,3,4,5,6,7,8,9,10]
      Enumeration e=v.elements();
6,8,10
      }
while(e.hasmoreElements())
      {     
            Integer I = (Integer)e.nextElement();
            if(I%2 == 0)
 
      System.out.println(v); //[0,1,2,3,4,5,6,7,8,9,10]



Limitations of Enumeration:
  • Enumeration Concept is Applicable Only for Legacy Classes and it is Not a Universal Cursor.  
  • By using Enumeration we can Perform Read Operation and we can't Perform Remove Operation.
To Overcome Above Limitations we should go for Iterator.

Iterator:
  • We can Use Iterator to get Objects One by One from Collection.
  • We can Apply Iterator Concept for any Collection Object. Hence it is Universal Cursor.
  • By using Iterator we can Able to Perform Both Read and Remove Operations.
  • We can Create Iterator Object by using iterator() of Collection Interface. 
    public Iterator iterator();

Methods:
  •  public booleanhasNext()
  •  public Object next()
  •  public void remove()


import java.util.ArrayList;
import java.util.Iterator;
 
public class IteratorDemo1 {
 
  public static void main(String args[]){
    ArrayList names = new ArrayList();
    names.add("Dashzin");
    names.add("Info");
    names.add("Blog");
 
    Iterator it = names.iterator();
 
    while(it.hasNext()) {
      String obj = (String)it.next();
      System.out.println(obj);
    }
  }
 
}



Limitations:  
  • ·   By using Enumeration and Iterator we can Move Only towards Forward Direction and we can’t Move Backward Direction. That is these are Single Direction Cursors but NotBiDirection.
  • ·   By using Iterator we can Perform Only Read and Remove Operations and we can't Perform Addition of New Objects and Replacing Existing Objects.

To Overcome these Limitations we should go for ListIterator.


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.
  • By using ListIterator we can Able to Perform Addition of New Objects and Replacing existing Objects. In Addition to Read and Remove Operations.
  • We can Create ListIterator Object by using listIterator().                         
                                 Public ListIterator listIterator();

Eg: ListIteratorltr = l.listIterator(); //l is Any List Object

Method :-

void add(int index,Object element)
It is used to insert element into the invoking list 
at the index passed in the index.
boolean addAll(int index,Collection c)
It is used to insert all elements of c into the 
 invoking list at the index passed in the index.
object get(int index)
It is used to return the object stored at the 
specified index within the invoking collection.
object set(int index,Object element)
It is used to assign element to the location 
specified by index within the invoking list.
object remove(int index)
It is used to remove the element at position 
index from the invoking list and return 
the deleted element.
ListIterator listIterator()
It is used to return an iterator to the start of 
the invoking list.
ListIterator listIterator(int index)
It is used to return an iterator to the invoking 
list that begins at the specified index.


import java.util.*;
classListIteratorDemo { 
public static void main(String[] args) {  
LinkedList l = new LinkedList();  
l.add("Dashzin");  
l.add("Info");  
l.add("Ankit");  
l.add("Naag");  
System.out.println(l);  
ListIteratorltr = l.listIterator();  
while(ltr.hasNext())
{   
String s = (String)ltr.next();   
if(s.equals("Venki"))    
ltr.remove();   
if(s.equals("Naag"))    
ltr.add("Chaitu");   
if(s.equals("Chiru"))    
ltr.add("Charan");  
}  
System.out.println(l); 
}
}



Comparison Table of 3 Cursors: 

Property
Enumeration
Iterator
ListIterator
Applicable For
Only Legacy Classes
Any Collection Objects
Only List Object
Movement
Single Direction (Only Forward)
Single Direction (Only Forward)
Bi-Directional
How To Get
By using elements()
By using iterator()
By using listIterator() of List(I)
Accessability
Only Read
Read and Remove
Read , Remove, Replace And Addition of New Objects
Methods
hasMoreElements(), nextElement()
hasNext(), next(),remove()

9Methods
Is it legacy?
Yes (1.0 Version)
No (1.2 Version)
No (1.2 Version)

DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment