Image Source on Google Search


Cursors Available in collection frame work

From the collection object to retrieve object we can use the following 3 cursors.

1. Enumeration
2. Iterator
3. ListIterator

Enumeration

This interface has introduced in 1.0 version it contains the following 2 methods.

boolean hasMoreElements();
Object nextElement();

Ex:

import java.util.*;
class EnumaretionDemo
{
public static void main(String arg[])
{
Vector v = new Vector();
for (int i =0;i<=10 ;i++ )
{
v.addElement(i);
}
System.out.println(v);
Enumeration e = v.elements();
while (e.hasMoreElements())
{
Integer i = (Integer)e.nextElement();
if((i%2) == 0)
System.out.println(i);
}
System.out.println(v);
}
}

Limitations of Enumeration

1. It is applicable only for legacy classes and it is not a universal cursor.
2. While iterating the elements by using enumeration we can perform only read operation and we can’t perform any modify/removal operations.

To overcome these problems we should go for Iterator interface.

Iterator

o Introduced in 1.2 version.
o We can get Iterator Object for any collection incremented class i.e it is universal cursor.
o By iterating the elements we can perform remove operation also in addition to read operation.

This interface contains the following 3 methods.

boolean hasNext();
Object next();
void remove();

Ex:

import java.util.*;
class IteratorDemo
{
public static void main(String arg[])
{
ArrayList al = new ArrayList();
for (int i =0;i<=10 ;i++ )
{
al.add(i);
}
System.out.println(al);
Iterator itr = al.iterator();
while (itr.hasNext())
{
Integer i = (Integer)itr.next();
if((i%2) == 0)
{
System.out.println(i);
}
else
{
itr.remove();
}
}
System.out.println(al);
}
}

Note:-

1. Enumeration and Iterator are single directional cursors. They can always move to words forward direction only.

2. By using Iterator we can perform read and remove operations. And we can’t perform any replace or addition of new objects

To over come these limitations we should go for list Iterator interface().

ListIterator

1.It has introduced in 1.2 version and it is child interface of Iterator.
2.It is a bidirectional cursor.
3.i.e Based on requirement we can move either to the forward or backward direction.
4.While Iterating we can perform replace and add operation in addition to read and remove this interface defines the following 9 methods.

1. boolean hasNext();
2. boolean hasPrevious();
3. Object next();
4. Object previous();
5. int nextIndex();
6. int previousIndex();
7. void remove();
8. void set(Object new)
9. void set(Object new)

nextIndex(): If there is no next element it returns size of the list.
previousIndex(): If there is no previous element it returns -1 .

import java.util.*;
class ListIteratorDemo
{
public static void main(String arg[])
{
LinkedList l = new LinkedList();
l.add("balakrishna");
l.add("chiru");
l.add("venky");
l.add("nag");
System.out.println(l);
ListIterator ltr = l.listIterator();
while (ltr.hasNext())
{
String s = (String)ltr.next();
if(s.equals("nag"))
{
ltr.add("chandra");
}
}
System.out.println(l);
}
}

The most powerful cursor is listIterator. But it’s main limitation is it is applicable only for list implemented classes (ArrayList, LinkedList, Vector, Stack).


Comparison between All the Three cursors



Properties
Enumeration
Iterator
ListIterator
1) It is Legacy?
Yes
No
No
2) It is applicable
Only for legacy classes
For any collection
Only for list
implemented classes
3) How to get?
By using elements()
By Using iterator()
By using listIterator()
4) Accessibility
Only read
read & remove
read/remove/replace/add
5) Movement
Single direction
(Only Forward)
Single direction
(Only Forward)
Biderctional
6) Methods
hasMoreElements()
nextElements()
hasNext()
next()
remove()
hasNext()
hasPrevious()
.
.
7) version
1.0
1.2
1.2

Post a Comment

saibrahma said... December 14, 2012 at 3:52 AM

Good Notes about the Cursors in Collections

 
Top