🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Iterator.html
Iterator (Java Platform SE 8 )
1 month ago - Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways: Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. Method names have been improved. This interface is a member of the Java Collections Framework.
🌐
GeeksforGeeks
geeksforgeeks.org › java › iterator-interface-in-java
Iterator Interface In Java - GeeksforGeeks
July 23, 2025 - Iterator is a universal iterator as it can be applied to any Collection object. We can traverse only in the forward direction using iterator. Using ListIterator which extends Iterator, can traverse in both directions.
🌐
Tutorialspoint
tutorialspoint.com › java › java_using_iterator.htm
Java - How to Use Iterator?
Each of the collection classes provides an iterator( ) method that returns an iterator to the start of the collection. By using this iterator object, you can access each element in the collection, one element at a time.
🌐
GeeksforGeeks
geeksforgeeks.org › java › iterable-interface-in-java
Iterable Interface in Java - GeeksforGeeks
2 weeks ago - Introduced in JDK 1.5 and part of java.lang package · Provides iterator(), spliterator(), and forEach() methods ... Here: T represents a generic type parameter in the Iterable interface.
🌐
Programiz
programiz.com › java-programming › iterator
Java Iterator Interface
The Iterator interface of the Java collections framework allows us to access elements of a collection. It has a subinterface ListIterator. All the Java collections include an iterator() method.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › Iterator.html
Iterator (Java SE 21 & JDK 21)
January 20, 2026 - Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways: Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. Method names have been improved. This interface is a member of the Java Collections Framework.
🌐
Medium
medium.com › @AlexanderObregon › beginners-guide-to-java-iterators-e8d4eb19bf5d
Java Iterators Guide For Beginners | Medium
March 15, 2024 - This pattern is part of the larger ... is realized through the Iterator interface, which specifies methods for querying and removing elements while iterating over collections....
🌐
DataCamp
datacamp.com › doc › java › iterator
Java Iterator
The Iterator interface provides three primary methods: hasNext(): Returns true if the iteration has more elements. It allows checking if there are more elements to iterate over. ... next(): Returns the next element in the iteration.
🌐
W3Schools
w3schools.com › java › java_iterator.asp
Java Iterator
This avoids repeating the long type name Iterator<String>, since the compiler already knows the type from the collection. This makes code shorter, but many developers still use the full type for clarity. Since var is valid from Java version 10, you may see it in other code, so it's good to know that it exists:
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › iterators-in-java
Iterator in Java - GeeksforGeeks
Iterator is part of the java.util package and is used by collection classes to traverse elements. Collection classes implement the Iterable interface, which provides the iterator() method that returns an Iterator object. ... next(): Returns ...
Published   March 11, 2026
🌐
Jenkov
jenkov.com › tutorials › java-collections › iterator.html
Java Iterator
May 22, 2020 - The Java Iterator interface represents an object capable of iterating through a collection of Java objects, one object at a time. The Iterator interface is one of the oldest mechanisms in Java for iterating collections of objects (although not ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › Iterator.html
Iterator (Java Platform SE 7 )
Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways: Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. Method names have been improved. This interface is a member of the Java Collections Framework.
🌐
University of San Francisco Computer Science
cs.usfca.edu › ~srollins › courses › cs112-f07 › web › notes › iterators.html
Iterators
An iterator is an object that has methods that allow you to proccess a collection of items one at a time. The java.util.Iterator interface provides the following methods: boolean hasNext() - Returns true if the iteration has more elements. E next() - Returns the next element in the iteration.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › Iterator.html
Iterator (Java SE 11 & JDK 11 )
October 20, 2025 - Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways: Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. Method names have been improved. This interface is a member of the Java Collections Framework.
Top answer
1 of 16
18

Why is this interface used?

Because it supports the basic operations that would allow a client programmer to iterate over any kind of collection (note: not necessarily a Collection in the Object sense).

Why are the methods... not directly coded to the data structure implementation itself?

They are, they're just marked Private so you can't reach into them and muck with them. More specifically:

  • You can implement or subclass an Iterator such that it does something the standard ones don't do, without having to alter the actual object it iterates over.
  • Objects that can be traversed over don't need to have their interfaces cluttered up with traversal methods, in particular any highly specialized methods.
  • You can hand out Iterators to however many clients you wish, and each client may traverse in their own time, at their own speed.
  • Java Iterators from the java.util package in particular will throw an exception if the storage that backs them is modified while you still have an Iterator out. This exception lets you know that the Iterator may now be returning invalid objects.

For simple programs, none of this probably seems worthwhile. The kind of complexity that makes them useful will come up on you quickly, though.

2 of 16
6

You ask: "Why are the methods hasNext(), next() and remove() not directly coded to the data structure implementation itself?".

The Java Collections framework chooses to define the Iterator interface as externalized to the collection itself. Normally, since every Java collection implements the Iterable interface, a Java program will call iterator to create its own iterator so that it can be used in a loop. As others have pointed out, Java 5 allows us to direct usage of the iterator, with a for-each loop.

Externalizing the iterator to its collection allows the client to control how one iterates through a collection. One use case that I can think of where this is useful is when one has an an unbounded collection such as all the web pages on the Internet to index.

In the classic GoF book, the contrast between internal and external iterators is spelled out quite clearly.

A fundamental issue is deciding which party conrols the iteration, the iterator or the client that uses the iterator. When the client controls the iteration, the iterator is called an external iterator, and when the iterator controls it, the iterator is an internal iterator. Clients that use an external iterator must advance the traversal and request the next element explicitly from the iterator. In contrast, the client hands an internal iterator an operation to perform, and the iterator applies that operation to every element ....

External iterators are more flexible than internal iterators. It's easy to compare two collections for equality with an external iterator, for example, but it's practically impossible with internal iterators ... But on the other hand, internal iterators are easier to use, because they define the iteration logic for you.

For an example of how internal iterators work, see Ruby's Enumerable API, which has internal iteration methods such as each. In Ruby, the idea is to pass a block of code (i.e. a closure) to an internal iterator so that a collection can take care of its own iteration.

🌐
Javatpoint
javatpoint.com › java-iterator
Java Iterator Interface - Javatpoint
class class is Hashtable and Linked ... iteration order. It inherits HashMap class and implements the Map interface. Points to remember contains values based on the key. contains unique elements. may have one null key and multiple null values. ... ... The stack is a linear data structure that is used to store the collection of objects. It is based on Last-In-First-Out (LIFO). In Java, the stack ...
🌐
Baeldung
baeldung.com › home › java › java collections › a guide to iterator in java
A Guide to Iterator in Java | Baeldung
June 27, 2025 - After reaching the end of the iteration, we can go backward to modify additional elements or simply print them from bottom to top. The Iterator interface allows us to modify a collection while traversing it, which is more difficult with a simple ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-implementing-iterator-and-iterable-interface
Java | Implementing Iterator and Iterable Interface - GeeksforGeeks
July 11, 2025 - Iterators are used in Collection framework in Java to retrieve elements one by one. For more details and introduction related to this, see this link. Why it is needed to implement Iterable interface?
🌐
Medium
medium.com › @Neelesh-Janga › iterator-and-iterable-in-java-be23f2b73125
Iterator and Iterable in Java — Java Stories | Neelesh J | Medium
July 7, 2024 - The Iterable interface acts as a root interface for all collection classes in Java. It defines a single method, iterator(), which allows objects to be iterated using the enhanced for-loop (foreach loop) or manually with an Iterator object.
Top answer
1 of 2
2

Is the "itr" not an object??

It's a reference.

Wasn't it impossible to make object of an interface??

You can not instantiate an interface. Here, a parent type (Iterator) reference is referencing an object of child type.

and what is that special thing

myPrecious.iterator(); ??

Here iterator is a function in the class whose object is myPrecious. Check the definition of the function, iterator here for an example.

wasn't it

new Iterator(); to instantiate an object??

You can instantiate a non-abstract class using the keyword, new. You can instantiate an anonymous class by using new on the interface name as shown here for an example.

2 of 2
0

The point of an interface is:

  1. You cannot directly create an instance of the interface, an object, you are correct.
  2. A class may implement the interface. It declares that it implements the interface and it contains all the methods that the interface contains (unless it’s an abstract class, but then again you can’t create objects from it, so let’s forget this situation for now).
  3. You may assign a reference to an instance (object) to a variable (or parameter) that is declared to have the interface type.

So to answer your questions:

Iterator itr = myPrecious.iterator();

Is the "itr" not an object?? …

myPrecious.iterator() returns a real object, and a reference to the object is stored into itr. The object probably belongs to some class that we haven’t heard of and do not need to care about. All that we know is that that class implements the Iterator interface. This means that we can use the iterator as specified by that interface.

wasn't it

new Iterator(); to instantiate an object??

Good question. Answer: In the end it is. However, very often in real-world programming we are calling an ordinary method in order to get a new object. That method must in turn use new. Or call another method that uses new (etc.). And this is where things go nicely hand in hand: since we don’t know the actual class of the object, we cannot use new ourselves. But myPrecious, your collection object, does know the class to use for instantiating an iterator object, so it can use new for us. And return the created iterator object to us.

One way to check is through this little code experiment:

    List<String> myList = new ArrayList<>();
    Iterator itr = myList.iterator();
    System.out.println(itr.getClass());

On my Java 11 it prints:

class java.util.ArrayList$Itr

Output on other java versions may be different. You notice that the output doesn’t mention the Iterator interface as the class of the iterator object, but instead some ArrayList$Itr. This means a class named Itr declared inside the ArrayList class. So yes, the iterator is really an object belonging to a class.