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);
}
}
}
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
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.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.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);
}
}
}
No comments:
Post a Comment