Before delving deeper, it’s important to know about the Java cursor. A Java Cursor is an Iterator. It is used to iterate or retrieve a collection or stream object’s elements one by one.
Java supports these four different cursors – Enumeration, Iterator, ListIterator, and Spliterator.
When one wants to display an element or to print some sequence, Java iterator comes into play. With this, one can get the required sequence without adding the print statement. Iterator is used for iterating (looping) various collection classes. Some of them are HashMap, ArrayList, LinkedList, etc.
Some people tend to get confused between the two – Iterator and Enumeration.
Iterator takes the place of Enumeration in the Java Collections Framework. But, there are certainly some differences between the two. Java iterator is an improved version of Enumeration.
Similarities between the two –
- Both iterate or traverse the collection object elements one by one.
- Both Enumeration and Iterator are java cursors.
- Both of them support read and remove operations.
- Both cater to forward iteration only. In short, both of them are uni-directional.
Differences between them –
- Enumeration employs lengthy method names. But an iterator has simple and easy method names.
- Enumeration has a legacy interface, and this isn’t the case of an iterator.
- Iterator is introduced in Java 1.2 and Enumeration in Java 1.0.
- Enumeration iterates only legacy collection classes. Iterator can be used to traverse any collection class.
- Iterator supports both read and remove operations. On the other hand, Enumeration supports only read operation.
- Iterator is a universal cursor, whereas the other isn’t.
Talking about Java iterator, it is an interface that belongs to a collection framework. It mainly has the following functions to perform –
- Access the data elements
- Allows the user to traverse the collection
- Removes the data elements of the collection.
Iterator allows the user to perform both read and remove operations. To create an iterator object, one can call it using Iterator ().
Characteristics of Java iterator
- It is used to iterate/traverse the elements of a collection object one by one.
- It is available since Java 1.2 Collection Framework.
- One can apply this for all Collection classes. It is because of this that it is also known as Universal Java Cursor.
- One of the biggest advantages of this is that it supports both read and remove operations.
- It is simpler and easier to use when compared to the Enumeration interface.
How to use an iterator?
One can follow the below steps to use an iterator for cycling through the contents of a collection.
- The first step is to get the Iterator to the start of the collection. One can do this by calling the collection’s Iterator ( ) method.
- The next step is such that it makes a call to hasNext( ). Simply put, the second step is catered to setting up a loop. Now, let the loop iterate as long as hasNext( ) returns true.
- Lastly, obtain each element by calling next( ) within the loop.
One must use Iterator whenever the need is to enumerate elements. It could be in two cases: the framework implemented interfaces like Set, List, Queue, etc. The other is implemented classes of Map interface. Iterator is the only cursor available for the entire collection framework.
Java iterator methods
Following are the Java iterator methods –
- hasNext() – This method is aimed at returning a true value. The condition is when a high number of elements are encountered during iteration.
- booleanhasNext() – When the iteration has more elements, it returns true. If this is not the case, then it returns false.
- next() – This will return the next element during the iteration. If there isn’t any, then it throws ‘NoSuchElementException’.
- remove()- This method removes the current element.
Advantages of Iterator in Java
Iterator in Java has the following advantages.
- It is easier than Enumeration.
- It is applicable for all the collection classes.
- Iterator in Java supports both reads as well as remove operations.
- While using for loop, one cannot add or remove the collection. But, with the help of an iterator, it is possible to do so. Therefore, you can now easily update the collection.
- It is very simple.
Limitations of Iterator in Java
Iterators in Java has the following drawbacks:
- It allows one to perform forward direction iteration only. In other words, this is Uni-Directional Iterator.
- It cannot handle a large volume of data with superior performance capabilities.
- If the target is to replace or add a new element, then this doesn’t work. It is because the Iterator doesn’t support this feature.
- When the user wants to use CRUD operations, it does not support create and update operations.
- It does not let the user iterate parallel elements. In other words, it supports only sequential iteration.
The Java Iterator interface allows the user to modify a collection while traversing it. Traditionally, using a simple for/while statement posed many difficulties.