Iterator and ListIterator in Java

Iterator and ListIterator are interfaces provided by Java API that are used to iterate over a Collection. They are both part of the Java Collection Framework. ListIterator is a sub-interface of Iterator and is specifically used to iterate of a List.

How to use an iterator?

Each of the collection implementation provides an implementation of an iterator that can be used to iterate over it. Below example show how to obtain an iterator from a collection object and use it to iterate over the collection.

Each collection object provide an iterator() method that can be used to obtain the iterator object. The hasNext() and next() method can be used to traverse over the elements of the collection as shown in the example below:
public class TestClass {

    public static void main(String[] args) {
     Set<String> set = new HashSet<String>();
     set.add("One");
     set.add("Two");
     set.add("Three");
     
     Iterator<String> iter = set.iterator();
     
     while(iter.hasNext()){
      System.out.println(iter.next());
     }
    }
}
Apart from the hasNext(), next() method it also contains a remove() method that can be used to remove an element from the collection.

Iterator vs ListIterator

1. Traversal Objects
Iterator can be used to iterate over any type of Collection like List, Set or a Map. Though we cannot directly iterate over a map implementation, we can however get the ketSet or the valueSet of the map and iterate over it.
ListIterator on the other hand can only be used to iterate over a List. It is a sub-interface of Iterator specifically meant to iterate over a List.
2. Traversal Direction
Iterator can be used to traverse only in forward direction.
ListIterator however can be used to traverse in both directions i.e. forward and backward. Apart from the hasNext() and next() methods ListIterator also has the hasPrevious() and previous() methods that can be used to traverse in the backward direction.
3. Modification while traversal
Iterator can only remove an element from the collection during traversal. Iterator has the remove() method which is used to remove an element from the collection at the current index during traversal.
Apart from removing an element, ListIterator also have the capability to add or replace an element during traversal. ListIterator provides the add() and set() methods to add and replace elements respectively.
4. Retrieve element index
Iterator does not provide any way to retrieve the index of the current element during traversal.
ListIterator provides the previousIndex() and nextIndex() methods that can be used to obtain the index of the element in the list while traversing.

Iterator vs Enumeration

Enumeration and Iterator are both used to traverse over the elements of a Collection, however there are some significant differences between the two.

Enumeration is a legacy class and is not supported by all the Collection object, it is only supported by the legacy collection classes like Vector and Hashtable. Iterator on the other hand is a standard class for traversing over a collection object and is supported by all collection implementation.

Enumeration does not support removal of elements during traversal, Iterator as we have already learnt supports removal of elements while traversal.
Iterators are more secure and safe as compared to Enumeration, since Iterator throws a ConcurrentModificationException in a multithreaded environment if a thread tried to modify a collection when another thread is iterating over it.
Below are the methods of the Enumeration
  • boolean hasMoreElements()
  • Object nextElement()
IF anyone want to learn Core Java Training in Jaipur. Please visit on  - Samyak IT Solutions Pvt Ltd

Comments

Popular posts from this blog

Cisco CCNA Certification Course Training in Jaipur

Industrial Training in Jaipur

Learn About Cisco and CCNA Training