QA's approach 2 JAVA - Collection Framework cont.. - Cursor Objects
Problems with Enumeration
Enumeration interface is used to iterate over legacy classes like Vector. It doesn't have the methods to add, remove, transverse in reverse direction, etc.
Methods available in Enumeration interface:
public boolean hasMoreElements()
public Object nextElement()
Iterator vs ListIterator
1) Iterator is used for traversing List and Set both.
We can use ListIterator to traverse List only, we cannot traverse Set using ListIterator.
2) We can traverse in only forward direction using Iterator.
2) We can traverse in only forward direction using Iterator.
Using ListIterator, we can traverse a List in both the directions (forward and Backward).
3) We cannot obtain indexes while using Iterator
3) We cannot obtain indexes while using Iterator
We can obtain indexes at any point of time while traversing a list using ListIterator. The methods nextIndex() and previousIndex() are used for this purpose.
4) We cannot add element to collection while traversing it using Iterator, it throws ConcurrentModificationException when you try to do it.
We can add element at any point of time while traversing a list using ListIterator.
5) We cannot replace the existing element value when using Iterator.
By using set(E e) method of ListIterator we can replace the last element returned by next() or previous() methods.
6) Methods of Iterator:
1) void add(Object arg0) : Inserts the specified element into the list (optional operation).
4) We cannot add element to collection while traversing it using Iterator, it throws ConcurrentModificationException when you try to do it.
We can add element at any point of time while traversing a list using ListIterator.
5) We cannot replace the existing element value when using Iterator.
By using set(E e) method of ListIterator we can replace the last element returned by next() or previous() methods.
6) Methods of Iterator:
public boolean hasNext()
public Object next()
public void remove()
Methods of ListIterator:
2) public boolean hasNext(): Returns true if this list iterator has more elements when traversing the list in the forward direction.
3) public boolean hasPrevious(): Returns true if this list iterator has more elements when traversing the list in the reverse direction.
4) public Object next() : Returns the next element in the list and advances the cursor position.
5) public int nextIndex() : Returns the index of the element that would be returned by a subsequent call to next().
6) public Object previous() : Returns the previous element in the list and moves the cursor position backwards.
7) public int previousIndex() : Returns the index of the element that would be returned by a subsequent call to previous().
8) public void remove() : Removes from the list the last element that was returned by next() or previous() (optional operation). 9) void set(E e): Replaces the last element returned by next() or previous() with the specified element (optional operation).
9) public void set(Object arg0): Replaces the last element returned by next() or previous() with specific element. This can be made only if neither of remove() or add(E) is not called after the last call.
Example for Trans-versing through arraylist using Iterator
import java.util.ArrayList;
import java.util.Iterator;
public class IteratorDemo3 {
public static void main(String args[]){
ArrayList<String> names = new ArrayList<String>();
names.add("Chaitanya");
names.add("Steve");
names.add("Jack");
Iterator<String> it = names.iterator();
while(it.hasNext()) {
String obj = it.next();
System.out.println(obj);
}
}
}
import java.util.Iterator;
public class IteratorDemo3 {
public static void main(String args[]){
ArrayList<String> names = new ArrayList<String>();
names.add("Chaitanya");
names.add("Steve");
names.add("Jack");
Iterator<String> it = names.iterator();
while(it.hasNext()) {
String obj = it.next();
System.out.println(obj);
}
}
}
Example for Trans-versing through arraylist
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class ListIteratorExample {
public static void main(String a[]){
import java.util.List;
import java.util.ListIterator;
public class ListIteratorExample {
public static void main(String a[]){
ListIterator<String> litr = null;
List<String> names = new ArrayList<String>();
List<String> names = new ArrayList<String>();
names.add("Shyam");
names.add("Rajat");
names.add("Paul");
names.add("Tom");
names.add("Kate");
names.add("Rajat");
names.add("Paul");
names.add("Tom");
names.add("Kate");
//Obtaining list iterator
litr=names.listIterator();
System.out.println("Traversing the list in forward direction:");
while(litr.hasNext()){
System.out.println(litr.next());
}
litr=names.listIterator();
System.out.println("Traversing the list in forward direction:");
while(litr.hasNext()){
System.out.println(litr.next());
}
System.out.println("\nTraversing the list in backward direction:");
while(litr.hasPrevious()){
System.out.println(litr.previous());
}
}
}
Comments
Post a Comment