Both Enumeration and Iterator is used for traversing through the underlying Collection. Enumeration can traverse limited collection such as Vectors and HashTable. Whereas Iterator can be used to almost all the Collection. Enumeration is twice as fast as Iterator as it act as read-only interface on the other hand Iterator is much safer as it doesn’t allow any modification to the underlying collection when iterating if any modification occurred it will throw ConcurrentModificationException and hence the Iterators are called as fail-fast Iterator as they fail quickly when it encounters a change.
Since Enumeration act as the read-only interface it can simply traverse through the Collection. It doesn’t have any methods to add or remove objects from the collection. Enumeration has 2 Major Methods to traverse hasMoreElements(),nextElement().
import java.util.Enumeration; import java.util.Vector; public class EnumerationEg { public static void main(String[] args) { Vector v = new Vector(); v.add("JavaInterviewPoint"); v.add("Java"); v.add("Interview"); v.add("Point"); Enumeration e = v.elements(); System.out.println("****Elements of the Collection****"); while (e.hasMoreElements()) { System.out.println(e.nextElement()); } }
You will get the below output
****Elements of the Collection**** JavaInterviewPoint Java Interview Point
Using the Iterator you can make modification the Collection which you are iterating. Iterator has remove() which lets you remove the object from the collection even when we are iterating without any ConcurrentModificationException.
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Remove_Iterator { public static void main(String args[]) { List al = new ArrayList(); al.add("1"); al.add("2"); al.add("3"); al.add("4"); al.add("5"); System.out.println("List before Iteration "+al); int indexFlag=0; Iterator it = al.iterator(); while(it.hasNext()) { it.next(); indexFlag++; if(indexFlag==2) { it.remove(); } } System.out.println("List after Iteration"+al); } }
This will not throw any exeception and you will get the below output.
List before Iteration [1, 2, 3, 4, 5] List after Iteration[1, 3, 4, 5]
Adding Element to the Collection when Iterating
Even there is one more iterator called as the ListIterator which will even let you to add the objects to the collection.
import java.util.ArrayList; import java.util.ListIterator; public class ListIteratorEg { public static void main(String args[]) { ArrayList test= new ArrayList(); test.add("j"); test.add("a"); test.add("v"); test.add("a"); ListIterator it=test.listIterator(); System.out.println("Contents of the arrayList Before adding "+test); while(it.hasNext()) { String link=it.next(); it.add("i"); } System.out.println("Contents of the arrayList After adding"+test); } }
it.add() method will let you add the element to the collection even when you are iterating
Contents of the arrayList Before adding [j, a, v, a] Contents of the arrayList After adding[j, i, a, i, v, i, a, i]
Leave a Reply