Programming |
Enumeration(I)
• It was introduced in Java 1.0
• It can only be used for legacy classes like Vector, Hashtable, Properties etc
• It is forward or uni-directional.
• elements() method is called on Collection object to get the enumeration
• hasMoreElements() to check if the enumeration has more elements
• nextElement() is used to get the next element
• It can be used to read the data only. Enumeration cannot be used to manipulate the data.
Program
public class VectorEnumerationExample {
public static void main(String[] args) {
// Creating a Vector
Vector<String> vector = new Vector<String>();
// Adding elements to Vector
vector.add("John");
vector.add("Mary");
vector.add("Joseph");
vector.add("Emma");
// creating Enumeration object
Enumeration<String> e = vector.elements();
// checking if more elements are there in Enumeration
while (e.hasMoreElements())
// Printing the next element
System.out.println(e.nextElement());
}
}
Output
John
Mary
Joseph
Emma
Iterator(I)
• It was introduced in Java 1.2
• This is also called as universal iterator as it can be used on any class implementing the collection interface. People will say that it can be used on maps as well but that is not correct because it is executed on entrySet() which itself is a set i.e. a class implementing the collection class.
• It is also single or forward or uni-directional like enumeration.
• The method iterator() is called on Collection to get the iterator type object.
• hasNext() method is used to check if more elements are there in the iterator.
• next() method is used to get the next element.
• Iterator can remove an element by using remove() method.
Program
public class IteratorExample {
public static void main(String[] args) {
// Creating a List of String
List<String> list = new ArrayList<String>();
// Adding elements to List
list.add("John");
list.add("Mary");
list.add("Joseph");
list.add("Emma");
// creating Iterator object
Iterator<String> iterator = list.iterator();
// Before iterating
System.out.println(list);
// checking if more elements are there in Iterator
while (iterator.hasNext()) {
String name = iterator.next();
if (name.equals("Joseph")) {
// removing element using remove() method
iterator.remove();
}
}
// After iterating and removing Joseph
System.out.println(list);
}
}
Output
[John, Mary, Joseph, Emma]
[John, Mary, Emma]
ListIterator
• It was introduced in Java 1.2
• This can only be used on classes implementing List interface like ArrayList, LinkedList etc.
• It is called as bi-directional iterator because it can iterate in both front and back directions.
• listIterator() method is called on List to get the ListIterator element.
• hasNext() method is used to check if more elements are there in forward direction
• hasPrevious() method is used to check if more elements are there in backward direction.
• next() method to get the next element.
• previous() method to get the previous element.
• nextIndex() method to get the integer index of next element.
• previousIndex() method to get the integer index of previous element.
• add() method to add an element after the current iterated element.
• remove() method to remove an element from the ListIterator.
• set() to replace an element with the current iterator element.
Program 1
public class ListIteratorExample {
public static void main(String[] args) {
// Creating a List of String
List<String> list = new ArrayList<String>();
// Adding elements to List
list.add("John");
list.add("Mary");
list.add("Joseph");
list.add("Emma");
list.add("Chad");
list.add("Kathy");
list.add("Joel");
list.add("Elle");
// creating ListIterator object
ListIterator<String> iterator = list.listIterator();
System.out.println("Forward Direction iteration");
// checking if more elements are there in Iterator
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println();
System.out.println("Backward Direction iteration");
// checking if more elements are there in Iterator
while (iterator.hasPrevious()) {
System.out.println(iterator.previous());
}
}
}
Output
Forward Direction iteration
John
Mary
Joseph
Emma
Chad
Kathy
Joel
Elle
Backward Direction iteration
Elle
Joel
Kathy
Chad
Emma
Joseph
Mary
John
Program 2
public class ListIteratorExample {
public static void main(String[] args) {
// Creating a List of String
List<String> list = new ArrayList<String>();
// Adding elements to List
list.add("John");
list.add("Mary");
list.add("Joseph");
list.add("Emma");
list.add("Chad");
list.add("Kathy");
list.add("Joel");
list.add("Elle");
// creating ListIterator object
ListIterator<String> listIterator = list.listIterator();
System.out.println("Before: " + list);
// checking if more elements are there in Iterator
while (listIterator.hasNext()) {
String name = listIterator.next();
if (name.equals("Mary")) {
// removing Mary
listIterator.remove();
}
if (name.equals("Chad")) {
// adding Jt after Chad
listIterator.add("JT");
}
if (name.equals("Joel")) {
// replacing Joel with Tommy
listIterator.set("Tommy");
}
}
System.out.println();
System.out.println("After: " + list);
}
}
Output
Before: [John, Mary, Joseph, Emma, Chad, Kathy, Joel, Elle]
After: [John, Joseph, Emma, Chad, JT, Kathy, Tommy, Elle]
Enumeration v Iterator v ListIterator
|
Enumeration(I) |
Iterator(I) |
ListIterator(I) |
Introduced in |
Java 1.0 |
Java 1.2 |
Java 1.2 |
Applicable on |
Legacy Classes |
Classes implementing Collection interface |
Classes implementing List Interface |
Direction |
Forward/Uni-directional |
Forward/Uni-directional |
Bi-directional |
Operation(s) |
Read |
Read and Remove |
Read, Remove, Replace, Add |
Method to get iterable object |
elements(); |
iterator() |
list.listIterator() |
Methods |
boolean hasMoreElements(); Object nextElement(); |
boolean hasNext(); Object next(); void remove(); |
boolean hasNext(); boolean hasPrevious(); Object next(); Object previous(); int nextIndex(); int previousIndex(); void remove(); void add(Object o); void set(Object o); |