Everything You Need To Know About Iterator in Java

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.

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

Full Stack Java Developer Course

The Gateway to Master Web DevelopmentExplore Course
Full Stack Java Developer Course

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.

Collection_framework

Different Methods Provided by Iterator in Java

The iterator in Java provides the following methods:

No.

Method Name

Description

1

boolean hasNext()

Checks for more elements and returns true, if found.

2

Object next()

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

3

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.

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.

UsesofIterator

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.

hasNext%28%29

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.

NoSuchElementException

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

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.

remove%28%29

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

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
Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

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 Online Java 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

SimplilearnSimplilearn

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.