Friday 6 January 2012

Retriving elements from Collections

The Three cursors in java:

                               If we want get the objects from one by one  from collection object we should go for cursors.
There are three type of cursor available in java
  • Enumeration
  • Iterator
  • ListIterator
Enumeration: 
                              We can use Enumeration to get objects one by one from legacy Collection objects. We can create Enumeration objects by using elements() method

Ex:
Vector v=new Vector();
Enumeration e=v.elements();

Enumeration interface defines the fallowing method:
  •  public boolean hasMoreElements();
  •   public objects nextElement();
Limitations of Enumeration:
  • we can  apply  Enumeration concept only for Legacy  classes and it is not a universal cursor.
  • By using  Enumeration we can get only read access  and we can't perform remove option
  • To overcome these limitations sun people introduced  "Iterator "  concept.

Enumeration Example:

import java.util.Vector;
import java.util.Enumeration;

public class EnumerationDemo {
 public static void main(String[] args) {
    //create a Vector object
    Vector v = new Vector();
 
    //populate the Vector
    v.add("One");
    v.add("Two");
    v.add("Three");
    v.add("Four");
   /Get Enumeration of Vector's elements using elements() method
    Enumeration e = v.elements();
  

    /*
      Enumeration provides two methods to enumerate through the elements.
      It's "hasMoreElements()" method returns true if there are more elements to
      enumerate through otherwise it returns false. Its" nextElement() "method returns
      the next element in enumeration.
    */
  
    System.out.println("Elements of the Vector are : ");
  
    while(e.hasMoreElements()){
      System.out.println(e.nextElement());

}
 

}
}

Output would be
Elements of the Vector are :
One
Two
Three
Four

Iterator:

  • We can use Iterator to get objects one by one from any collection object.
  • we can apply  Iterator concept for any Collection object and it is a universal collection
  • While iterating objects by iterator we can perform both read and write operations.

We can get Iterator objects by using iterator() method of Collection interface.

Iterator i=c.iterator(); //"C" any collection object

Iterator interface defines the fallowing methods
  • public boolean hasNext();
  • public object next();
  • public void remove();
Limitations of Iterator:
  • Both Enumeration and Iterator are single direction cursors only.i.e we can always move only forward direction only.we can't move backward direction.
  • While iterating by iterator we can perform only read and remove operation but we can't perform replace and addition operation on that object.
  • To overcome these drawbacks we go for ListIterator.
Iterator Example:

import java.util.Iterator;
import java.util.ArrayList;

public class IteratorDemo {

  public static void main(String[] args) {

    //create an ArrayList object
    ArrayList aList = new ArrayList();
  
    //adding elements to  ArrayList object
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");

    aList.remove(2);//we are remove the element from specific position

    Iterator itr = aList.iterator();
 //iterate through the ArrayList values using Iterator's hasNext and next methods
while(itr.hasNext()){
      System.out.println(itr.next());
   }

}
}
Output would be
1
2
3
5


ListIterator: 

       ListIterator  is the child interface  of  Iterator.
                                    By using  ListIterator  we can move both  in forward direction  and backward direction.i.e it is a bidirectional cursor.
While iterating by ListIterator we can perform replacement and addition of new objects in addtion to read and remove operations.
By using listIterator() method we can create ListIterator object.

ListIterator itr = l.listIterator();
ListIterator defines the fallowing methods.

  • public boolean hasNext();
  • public object next();
  • public int  nextIndex();
  • public boolean  hasPrecious();
  • public object  previous();
  • public void previousIndex();
  • public void remove();
  • public void set(Object o);
  • public void add(Object new);




























No comments:

Post a Comment