Skip to content

Java Iterator Tutorial with Examples

Java Iterator tutorial with examples will help you to understand how to use the Java Iterator in an easy way. Iterator interface in Java is a member of the Java Collection Framework. Java Iterator is used to iterate over a collection to retrieve its elements.

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

How to iterate over a Collection using the Iterator?

The Iterator in Java defines three main 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 object for the collection by calling the iterator method.
  2. Loop until the Iterator’s hasNext method returns true.
  3. Inside the loop, call the next method of the Iterator to get the current element of the collection.

The below given code snippet shows how to iterate over an ArrayList using an iterator.

Output

Important note:

Do not call the next method without first checking whether the collection has more elements using the hasNext method. Also, do not call the next method multiple times inside the loop to make sure that there will always an element. The next method will throw NoSuchElementException if there are no elements to iterate.

Consider below given code snippet.

What do you think the output would be? If you think that the code will print names and marks for all the students, you are wrong. Here is the output of the code.

The code prints the name of the first student followed by the marks of the second student. Again, it prints the name of the third student and the marks of the fourth student. It is because we have called the next method twice in the loop. When you call the next method on the iterator object, it returns the next element of the collection. When the iterator is on the fifth element, we call the next method and since there are no more objects in the collection, the code throws NoSuchElementException.

Here is how the above code should be written.

Output

Also, there should be one next method call per hasNext method to avoid NoSuchElementException.

How to iterate over ArrayList in Java?

Output

Please visit the ArrayList tutorial to know more about ArrayList.

How to iterate over LinkedList in Java?

Output

Please visit the LinkedList tutorial to know more about LinkedList.

How to iterate over TreeSet in Java?

Output

Please visit the TreeSet tutorial to know more about TreeSet.

How to iterate over HashSet in Java?

Output

Please visit the HashSet tutorial to know more about HashSet.

How to iterate over HashMap in Java?

Iterator through keys of HashMap

Output

Iterator through values of HashMap

Output

Iterate through key-value mappings of HashMap

Output

Please visit the HashMap tutorial to know more about HashMap.

How to iterate over an TreeMap in Java?

Iterator through keys of TreeMap

Output

Iterator through values of TreeMap

Output

Iterate through key-value mappings of HashMap

Output

Please visit the TreeMap tutorial to know more about TreeMap.

How to iterate over an LinkedMap in Java?

Iterator through keys of LinkedHashMap

Output

Iterator through values of LinkedHashMap

Output

Iterate through key-value mappings of LinkedHashMap

Output

Please visit the LinkedHashMap tutorial to know more about LinkedHashMap.

How to remove elements using the Iterator?

The remove method of the Iterator class removes the current element from the collection.

Output

Note: The remove method throws IllegalStateException if,

  • The remove method is called before the first call of the next method.
  • The remove method is called the second time for the same element.

Also remove method is not supported by all the collections. If you call the remove method on a collection which does not support the remove operation, it will throw UnsupportedOperationException.

For example, the list obtained by converting an array object using the asList method of the Arrays class does not support the remove operation.

Output

Below given are additional Java iterator examples that discuss topics in more detail.

Java Iterator Examples

References:
Java Iterator interface Javadoc

Please let me know if you liked the Java Iterator tutorial with examples in the comments section below.

About the author

Leave a Reply

Your email address will not be published.