Iterator in Java is an object used to cycle through arguments or elements present in a collection. It is derived from the technical term “iterating,” which means looping through. Generally, an iterator in Java is used to loop through any collection of objects. To apply the iterator, all you need to do is import the java.util package and then use the iterator() method. You can then use the iterator to perform multiple operations in the collection.

This tutorial will look at numerous applications of an iterator in Java with examples and its drawbacks. But before diving into the details of the iterator, let’s learn what a Collection framework (on which the iterator() method operates) is.

Get All Your Questions Answered Here!

Caltech PGP Full Stack DevelopmentExplore Program
Get All Your Questions Answered Here!

What is a Collection Framework?

The Collection framework in Java provides an architecture to store many values or objects in a single unit. You can apply all sorts of operations required to be performed on data, such as adding, removing, altering, searching, and sorting using the Collection framework method. The Java Collection Framework provides the following interfaces:

  • Set
  • List
  • Deque
  • Queue

It also provides the following classes:

  • ArrayList
  • HashSet
  • Vector
  • LinkedList
  • PriorotyQueue
  • TreeSet
  • LinkedHashSet

The next step is to create an ArrayList with the Collection framework.


Different Methods Provided by Iterator in Java

The iterator in Java provides the following methods:


Method Name



boolean hasNext()

Checks for more elements and returns true, if found.


Object next()

It returns the next object or element in the collection. If no more elements are present, it throws a NoSuchElementException error.


void remove()

It can only be used after the next() method and removes the current object where the iterator is present. If you try to call the remove() method without calling the next() method, it throws an IlegalStateException error.

Get the Coding Skills You Need to Succeed

Full Stack Development-MEANExplore Program
Get the Coding Skills You Need to Succeed

Uses of Iterator in Java

Now that you know about the Collection framework and the methods provided by the iterator in Java, it’s time to apply them. The iterator is commonly used to loop through the objects of a collection, read them, and remove them. Please go through each application with an original example, for better clarity. But before that, you will look at an example to call an iterator and print the first item in the collection. By using the same “fruits” collection which was created in the Collection framework section, we will explore this topic.


Example to use hasNext() method

In this example, you will use the hasNext() method with the while loop to iterate through the “fruits” ArrayList. It will also see the use of the next() method to print each element on a separate line.


Example to use next() method and see the NoSuchElementException

In this example, you will use the next() method of iterator in Java to loop through all the elements of the collection. After reaching the last element, the next() method will be used again, to get the NoSuchElementException error.


Example of using remove() method and see IllegalStateException

In this example, you will use the remove() method of iterator in Java to remove one of the elements from the collection. But this time, try it with an integer data type ArrayList.


Similar to the below example, use the remove() method before calling the next() method. Hence, it throws the IllegalStateException.


Limitations of Iterator in Java

Iterators are useful to perform many operations in a collection, but there are also some drawbacks to it. The primary limitations of iterators in Java include:

  • It does not support backward direction iteration
  • It doesn’t enable you to add new elements to the collection
  • It does not support update operations on the collection

Learn From The Best Mentors in the Industry!

Automation Testing Masters ProgramExplore Program
Learn From The Best Mentors in the Industry!


In this post, you have explored what an iterator in Java is, its uses, limitations, and methods. It also explored what exceptions you can come across while using iterators. In case you cannot get out of an exception, learning about exception handling in Java will come in handy. Similarly, you can learn about various Java concepts by taking Simplilearn’s Full Stack Certification Course. The course is apt for learning basic and advanced concepts and getting hands-on experience to become a Java developer.

Have any questions for us? Leave them in the comments section of this article, and our experts will get back to you on them, as soon as possible!

About the Author


Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.