Java Iterator and its hasNext and next methods
Java

Java Iterator Over Collection

The Java Iterator is a reference over a collection object. It can be ArrayList, LinkedList anything which implements the basic Collection Interface. With the iterator we can get all the items in the collection one by one. We can iterate over the collection via a loop or via a for-each construct. But, using these, we cannot adjust the structure while the iteration is in-progress. Whereas with Java Iterator, we can remove items while the iteration is going on. Java Iterator is a Forward-Only Cursor. This means one can iterate the collection items only in forward direction. Standard Java Iterator does not allow the reverse way of iteration. Also, note the Standard Java Iterator does not allow adding the items even though it allows removal. In this example, we will learn about the Iterator using an ArrayList collection object.