- 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.
- Enumeration
- Iterator
- ListIterator
Methods:
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]
- 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.
- 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.
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);
}
}
}
- · 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.
- 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().
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.
|
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)
|
0 comments:
Post a Comment