Java Iterator interface is a member of Java Collection Framework. Java Iterator is an iterator over a collection which is used to iterate over a collection to retrieve the collection elements.

Unlike Enumeration, Iterator allows us to remove elements from underlying collection while iterating over the elements of the collection.

Java Iterator defines three methods. The hasNext method returns boolean depending upon whether there are more elements to iterate over in the collection. The next method returns the next element of the collection. The remove method removes the current element from the underlying collection.

The iterator method is used to get the Iterator for the collection class, for example, implementations of Set and List interfaces. Here is how to iterate over a collection.
1) Get the Iterator for the collection by calling the iterator method.
2) Loop until the Iterator’s hasNext method returns true.
3) Inside the loop, call next method of the Iterator to get the current element of the collection.

Below given are some of the Java Iterator examples which shows how to use Iterator in Java.

Java Iterator Examples

Join 1000+ fellow learners! Enter your email address below: