Friday, January 5, 2018

Understand the difference among the Java cursors by using their properties

As we have discussed cursors defination ,  below are the differentiation among the cursors by using their properties.

Properties Enumeration Iterator ListIterator
1. where we can apply legacy classes only any collection object only for List object
2. Its legacy ? Yes(1.0 version) No(1.2 version) No(1.2 version)
3. Movement directioin single direction forward single direction forward both direction
4. Allowed opeation only read only read , remove read/remove/replace/add
5. how we can get ? by using elements() of Vector class by using iterator() of Collection interface by using listIterator() of List Interface
6. methods hashMoreElements() hashNext()  having 9 methods for both direction
nextElement() next()
remove()


Internal implementation of cursors Example :

package indrajeet.singh.cursors.imp;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;

public class CursorsDemo {

public static void main(String[] args) {
Vector v=new Vector();
Enumeration e=v.elements();
Iterator itr=v.iterator();
ListIterator litr=v.listIterator();

System.out.println(e.getClass().getName());
System.out.println(itr.getClass().getName());
System.out.println(litr.getClass().getName());


}

}

Output:
java.util.Vector$1
java.util.Vector$Itr
java.util.Vector$ListItr

Thursday, January 4, 2018

Collection Cursors in Java

There are three types of cursors in Collection. which are used for getting element one by one.
 1. Enumeration -> Legacy classes
 2. Iterator          -> any collection (universal cursor)
 3. ListIterator    -> List

If we want to get objects one by one from the collection in the java then we should go for  above cursors.

 1. Enumeration :

We can use Enumeration to get objects one by one from the legacy classes.
We can enumerate objects by using Elements() method of Vector class.

public Enumeration elements();

Example : Enumeration e=v.elements().

 v is a vector object.

methods:
public Boolean hasMoreElements();
public object nextElement();

Example :

package indrajeet.list.exp;

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

public class VectorExp {

public static void main(String[] args) {


Vector v=new Vector<>();
System.out.println(v.capacity());
v.add("indraeet");
v.addElement("singh");
v.add(1, 4);
System.out.println(v);
v.removeElementAt(1);

System.out.println(v);
System.out.println(v.size());

Enumeration e=v.elements();
while(e.hasMoreElements())
{
System.out.println( e.nextElement());
}

}

}

Output:
10
[indraeet, 4, singh]
[indraeet, singh]
2
indraeet
singh
                         

Limitations of Enumeration:

1.  We can apply Enumeration only for legacy classes and its not a universal cursor.
 2. by using Enumeration , we can get read only access not remove operation.


to overcome above limitation we should go for Iterator cursor.

2. Iterator cursor.

 We can apply Iterator  cursor for any collection object So we can called Iterator cursor as a universal cursor.
We can perform both read and remove operations by using Iterator cursor.
We can create Iterator object by using iterator() method of Collection interface.
public Iterator iterator();

Example : Iterator i= c.iterator();
where c is a Collection object.

Methods:
 1. public boolean hasNext();
 2. public object next();
 3. public void remove();

package indrajeet.list.exp;

import java.util.*;

public class CursorsExp {

public static void main(String[] args) {

ArrayList al=new ArrayList<>();

al.add("anu");
al.add("indrajeet");
al.add("singh");
al.add("tomar");
al.add("golu");
al.add("singh");

Iterator itr=al.iterator();

for(Object s: al)
{
System.out.println(itr.next());

}

}

}


Limitations of Iterator cursor:

 1. We can only move forward direction by using Iterator cursor. We can not move towards backward       direction So its only single direction cursor not a bidirectional.
 2. By using Iterator , we can  perform only read/remove operations not replacement  and addition           operations of  new object operation.

So overcome above limitations we should go for ListIterator cursor.

3. ListIterator cursor: 

   1. We can move both direction forward and backward So we can say its a bidirectional cursor.
   2. We can perform read , remove , write  and replacement operation also and also addition of a new         object in a collection.

  public ListIterator listIterator();
  ListIterator ltr=l.listIterator();

  where l= any List object.

Methods:

1. Forward movement:

 1. public boolean hasNexr();
 2. public object next();
 3. int nextIndex();

2. backward movement:
  1. public boolean hasPrevious();
  2. public object previous();
  3. public int previousIndex();

3. Extra operations(Replacement and add)
    1. pubic void remove();
    2. public void add(object o);
    3. public void set(object o);

below is the Example for ListIterator :


package indrajeet.list.exp;

import java.util.*;

public class CursorsExp {

public static void main(String[] args) {

ArrayList al=new ArrayList<>();

al.add("anu");
al.add("indrajeet");
al.add("singh");
al.add("tomar");
al.add("golu");
al.add("singh");

System.out.println(al);
ListIterator li= al.listIterator(); // ListIterator code
while(li.hasPrevious())
{
String o=(String) li.previous();
if(o.equals("goluu"))
{
li.remove();

}
else
{
li.add("goluaa");
}
System.out.println(al);
}

}

}