Showing posts with label Iterator. Show all posts
Showing posts with label Iterator. Show all posts

Difference between Iterator and Enumeration in Java?

In our last tutorial we have seen difference between Iterator and ListIterator. In this tutorial we will see about difference between Iterator and Enumeration in Java. Both Iterator and Enumeration are used to traverse elements in the Collection Object. Differences are,
  • Enumeration are introduced in older Java version and Iterator introduced in later version with additional features.
  • By Enumeration allows only traverse through elements, where as by Iterator we can traverse and also we can remove elements from the Collection Object.
  • Iterator is more secure and safe compared to Enumeration since its thread safe and won't allow others threads to modify the elements in Collection Object while some Thread is Iterating. On that case it will give ConcurrentModificationException

Finally we can decide if we are going for Read-only then we can for Enumeration else we can go for Iterator.

Mainly there are two types of Iterator in Java and they fail-fast and fail-safe Iterators. fail-fast iterators are those who throw ConcurrentModificationException if collection Object is modified when other Thread iterating on same Object. Fail-safe Iterator works on copying collection Object and won't work on original Object and its safe compared to fail-fast.    

Lets see example for both Iterator and Enumeration in Java



public class IteratorEnumerationTest {
 
 public static void main(String[] args) {
  
  ArrayList<String> myList = new ArrayList<String>();
  
  myList.add("one");
  myList.add("two");
  myList.add("three");
  myList.add("four");
  myList.add("five");
  
  Iterator<String> itr = myList.iterator();
  System.out.println("THROUGH ITERATOR : ");
  while(itr.hasNext()){
   System.out.print(itr.next()+", ");   
  }
  
  Vector<String> vec = new Vector<String>(myList);

  Enumeration<String> enu = vec.elements();
  
  System.out.println("\n\nTHROUGH ENUMERATION : ");
  while(enu.hasMoreElements()){
   System.out.print(enu.nextElement()+", ");
  }
 } 
}



OUTPUT:


THROUGH ITERATOR : 
one, two, three, four, five, 

THROUGH ENUMERATION : 
one, two, three, four, five, 










Difference between Iterator and ListIterator in Java?

In this tutorial we will see about difference between java.util.Iterator and java.util.ListIterator interfaces. Both interface are used to traverse values from the collection Object. But the differences are 

  • Iterator can traverse only in forward direction, where as ListIterator can traverse in both (bidirectional). 
  • Iterator can be used to traverse values in List and Set collections, where as ListIterator is used to traverse only in List.
  • ListIterator derived from Iterator interface and contains functions like remove(), previous(), next(), nextIndex(), previousIndex() etc.,


Lets see examples for both Iterator and ListIterator as how its used on Collection Object. 

Iterator Example:


public class IteratorTest {
 
 public static void main(String[] args) {
  List<String> myList = new ArrayList<String>();
  myList.add("java");
  myList.add("collections");
  myList.add("API");
  myList.add("important");
  myList.add("interview");
  myList.add("questions");
  
  System.out.println("LIST BEFORE REMOVE : "+myList);
  
  Iterator<String> itr = myList.iterator();
  while(itr.hasNext()){
   String val = itr.next();
   if(val.equals("API")){
    // Removing "API" value from ArrayList
    itr.remove();
   }   
  }  
  System.out.println("LIST AFTER REMOVE : "+myList);
 } 
}

OUTPUT:


LIST BEFORE REMOVE : [java, collections, API, important, interview, questions]
LIST AFTER REMOVE : [java, collections, important, interview, questions]


ListIterator Example:


public class ListIteratorTest {
 
 public static void main(String[] args) {
  List<String> myList = new ArrayList<String>();
  myList.add("java");
  myList.add("collections");
  myList.add("API");
  myList.add("important");
  myList.add("interview");
  myList.add("questions");
  
  System.out.println("LIST : "+myList);
  
  ListIterator<String> itr = myList.listIterator();
  while(itr.hasNext()){
   String val=itr.next();   
   if(val.equals("API")){
    // moving to previous element
    itr.previous();
    System.out.println("PREVIOUS VALUE : "+myList.get(itr.previousIndex()));
    // moving to forward element
    itr.next();
    System.out.println("NEXT VALUE     : "+myList.get(itr.nextIndex()));
   }
  }
 } 
}

OUTPUT:


LIST : [java, collections, API, important, interview, questions]
PREVIOUS VALUE : collections
NEXT VALUE     : important