Iterator ListIterator and Enumeration
SR
|
Iterator
|
List Iterator
|
Enumeration
|
1
|
iterator was introduced after Enumerator
|
Enumeration is older and its there from JDK1.0
| |
2
|
is Iterator has a remove() method
|
is ListIterator has a remove() method
|
while Enumeration doesn't.
|
3
|
Iterator we can manipulate the objects like adding and removing the objects from collection e.g. Arraylist.
|
Enumeration acts as Read-only interface, because it has the methods only to traverse and fetch the objects,
| |
4
|
Iterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException.
|
ListIterator is more secure and safe as compared to Enumeration because it does not allow other thread to modify the collection object while some thread is iterating over it and throws ConcurrentModificationException
|
Less secure as compared to Iterator
|
5
|
Iterator interface methods:
hasNext()
next()
remove()
|
LisIterator hods:
hasNext()
next()
remove()
hasPrevious()
previous()
|
Enumeration
hasMoreElement()
nextElement()
N/A
|
6
|
We can iterate only in one direction .
|
We can iterate in both direction .
|
We can iterate only in one direction .
|
7
|
Iteration can be done only once. If you reach the end of series its done. If we need to iterate again we should get a new Iterator.
|
New ListIterator is not required
|
Enumeration can be done only once. If you reach the end of series its done. If we need to iterate again we should get a new E.numerator
|
8
|
The interface Java.util.ListIterator extends java.util.Iterator interface.
| ||
9
|
public class Test {
public static void main (String args[])
{
List<String> list =new ArrayList();
list.add("01");
list.add("02");
list.add("03");
list.add("04");
Iterator LS= list.Iterator();
while (LS.hasNext()) {
String s=(String) LS.next();
System.out.println(s);
}
}
}
|
public class Test {
public static void main (String args[])
{
List<String> list =new ArrayList();
list.add("01");
list.add("02");
list.add("03");
list.add("04");
ListIterator LS= list.listIterator();
while (LS.hasNext()) {
String s=(String) LS.next();
System.out.println(s);
}
}
}
|
public class Test {
public static void main (String args[])
{
List<String> list =new ArrayList();
list.add("01");
list.add("02");
list.add("03");
list.add("04");
//get the Enumeration object
Enumeration e = Collections.enumeration(list);
//enumerate through the ArrayList elements
System.out.println("Enumerating through Java ArrayList");
while(e.hasMoreElements()){ System.out.println(e.nextElement());
} }
}
|
No comments:
Post a Comment