Java Iterator Interface of java collections allows us to access elements of the collection and is used to iterate over the elements in the collection(Map, List or Set). It helps to easily retrieve the elements of a collection and perform operations on each element. Iterator is a universal iterator as it can be applied to any Collection object. We can traverse only in the forward direction using iterator. Using ListIterator which extends Iterator, can traverse in both directions. Both read and remove operations can be performed by the iterator interface. This is included in Java JDK 1.2. The only Enumeration is the first iterator to be included in JDK 1.0. To use an iterator, we must import java.util package.
Limitations of Enumeration Interface:
An Iterator interface is used in place of Enumeration in Java Collection.
- Enumeration is not a universal iterator and is used for legacy classes like Vector, Hashtable only.
- Iterator allows the caller to remove elements from the given collection during iterating over the elements.
- Only forward direction iteration is possible in an Enumeration.
Declaration of iterator interface
public interface Iterator<E>
E – the type of elements returned by this iterator
Subinterfaces of Iterator
public interface EventIterator extends Iterator<Event>
EventIterators are unmodifiable.
Methods: nextEvent() which returns the next Event in an EventSet.
public interface ListIterator<E> extends Iterator<E>
An Iterator for lists which allows to traverse the list in either of the forward or backward direction or modify the list during the iteration and to obtain the current position of the iterator. ListIterator has no current element.
PrimitiveIterator<T,T_CONS>, PrimitiveIterator.OfInt, PrimitiveIterator.OfLong
Example: Implementation of Iterator
All classes in the Collection Framework provide iterator() method which returns the instance of Iterator to iterate over the elements in that collection.
The list is given as : [Welcome, to, GFG] Welcome to GFG After the remove() method is called : [Welcome, to]
ArrayList Iterator Example
10 20 30 40 50 60 70 80
Develop Custom Class Iterator
To provide similar functionality for user-defined /custom class, we should follow the below steps:
- Define a custom class.
- Define the collection class to this custom class.
- The collection class should import java.util package and implement iterable interface.
- This collection class should now provide implementation to Iterable interface’s method iterator().
Example code of developing custom class:
practice geeks for geeks to learn coding
Using remove() method to remove items from a collection
- It removes the last element of the collection returned by the iterator.
- If the iteration is in progress and meanwhile underlying collection is modified then by calling remove() method, an Iterator will throw a ConcurrentModificationException.
Iterator forEachRemaining() Example
10 20 30 40 50 60 70 80
Advantages of Java Iterator:
- It is not a legacy interface and can traverse overall collections like ArrayList, HashMap, TreeSet, HashSet etc.
- It can be used for any java collection and therefore known as Universal Cursor for Collection API.
- Read and Remove operations are supported.
- Simple and easily usable method names.
Limitations of Java Iterator:
- It does not support Create and Update operations in CRUD (Create, Read, Update, Delete) operations.
- It supports only uni-directional traversal i.e in forwarding direction.
- It does not support a better iteration on a large volume of data in comparison to Spliterator.
- It supports only sequential iteration i.e it does not support iterating elements parallel.
Difference between Iterator and Enumeration:
|It was introduced in JDK 1.2.||It was introduced in JDK 1.0.|
|It is a universal Cursor.i.e can be used in any java collections.||It is not a universal cursor.i.e we can use it only for some legacy classes.|
|It supports both Read and Remove operations.||It supports only Read operation.|
|It has simple method names.||It has lengthy method names.|
|One can do any modification while traversing over the elements.||We cannot do any modifications while traversing.|
|Not a legacy interface. Can traverse overall collections like ArrayList, HashMap, TreeSet, Vector etc.collections||Legacy interface. Traverse only Vector, Hashtable.|
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.