iterator
Last updated
Was this helpful?
Last updated
Was this helpful?
An iterator in Java is an object used to loop through collections of objects. It is derived from the technical term "iterating," which means looping through. An iterator is an interface that is used to traverse a collection of Java object components one by one. It is a universal cursor that can be applied to any Collection object. To use an iterator, you must import it from the java.util package. The iterator() method can be used to get an Iterator for any collection. The hasNext() method checks if there is another element in the collection that can be iterated over, the next() method returns the next element in the collection, and the remove() method removes the current element from the collection. The Java Iterator is also known as the universal cursor of Java as it is appropriate for all the classes of the Collection framework. When a user needs to use the Java Iterator, then it's compulsory for them to make an instance of the Iterator interface from the collection of objects they desire to traverse over.