Iterator for singly linked list java

In this post, we are going to implement the Iterator for Singly Linked List in JAVA. Iterator implementation is a very important feature of any linear data structures. It provides the capability to use JAVA’s “for loop”, “enhanced for loop” and “for-each” functional programming.

We are going to use the same Singly Linked List class from all the previous posts. Below are the steps to implement Iterator for singly linked list.

  1. Implement java.lang.Iterable<T> interface to custom SinglyLinkedList class.
  2. After implementing Iterable, we need to override the method, “public Iterator<T> iterator()“. This method should return the Iterator object for Singly Linked List.
  3. To provide the Iterator object, we need to implement the java.util.Iterator<T> to Singly Linked List class. After implementing Iterator, we need to override the two methods, public boolean hasNext() and public T next(). We will see the implementation of these two methods separately.
  4. Now, return the self-reference of SinglyLinkedList class in overridden method public Iterator<T> iterator().

Since SinglyLinkedList class implements Iterator<T>, so class itself will be an Iterator type. Hence we are returning the same class reference in overridden iterator() method

To Implement the iterator for singly linked list, we should provide the implementation for hashNext() and next() method. Internally, JAVA calls these methods to check the availability of data and fetch the next data from any linear collections if class implements Iterable.

Iterator design pattern works on Iterator point. Iterator point defines the current position of iterator reference start from one end of the collection. In singly linked list, we can traverse through the data in forward direction so our iterator point will start from root node.

Default : iteratorPointer = rootNode;

hasNext() : It will check if node is available on iterator point. If node on iterator point is not null , return TRUE else returns FALSE.

next() : Return the data on the iterator point.

Iterator point is the key member for Iterator design pattern. We need to properly manage the iterator point on any modification done on singly linked list. As we seen, default position of iterator point is root node, so we need to reset the iterator point every time when we add/delete/modify the singly linked list. To do so, we will have one more method called resetIteratorPointer() where we simply assign the iterator pointer as root node.

Now, we need to call the method resetIteratorPointer() every time whenever we add/delete/modify the singly linked list. We are going to add this method call in all the add/delete methods written for singly linked list. Refer the highlighted lines.

Along with the Iterator design, we should handle the concurrent modification scenario. We should not allow any modification to singly linked list if Iterator Pointer is moving. Refer the method  checkForConcurrentModification() .

Node.java

SinglyLinkedList.java

Airport.java : Java DTO class

 

Below is the test class where we will print the Route using enhanced-for loop, forEach and Iterator. Also, we will try to delete particular airport(“SFO”) while iterating the singly linked list. In this case, we will get ConcurrentModificationException.

 

Result:

 

Hope you like this post. Please leave your comments/suggestions.

You may like –

 

Top