kTiwari
kTiwari

Reputation: 1508

Implementation of Iterator methods

I am working with collections. One thing which is bothering me is: where is the Implementations of the methods of java.util.Iterator Interface? In which class these methods are implemented?

 public abstract boolean hasNext();
 public abstract E next();
 public abstract void remove();

I searched the source code of the java API, but didn't find the implementation of these methods in any class.

Upvotes: 1

Views: 817

Answers (4)

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

There are multiple classes in JDK where It has been implemented. ArrayList is very good example for your concern. You can go through the code in openJDK. And the iterator method defination is -

  public Iterator<E> iterator() {
         return new Itr();
     }

Where This Itr private class implements Iterator<E> and define all itarator method.

Upvotes: 1

Bharat Sinha
Bharat Sinha

Reputation: 14363

In case you are using eclipse and you have source code configured in eclipse itself.

Just select the method and press Ctrl + T (show type hierarchy) and you can see all the classes in which the method has been implemented.

Upvotes: 0

zhaoyw
zhaoyw

Reputation: 31

You can search java apis who implements Iterator. Those classes all have implements the above methods. Go to browse the jdk source code. It will help you a lot.

Upvotes: 0

Gopal
Gopal

Reputation: 86

Iterator is an interface and it has around 50 implementations in the java api itself. Since the iterator needs to compy with the iterating object type, for ex if you want to iterate an ArrayList the iterator instance which your iterator() method returns is of new Itr type. see the implementation in java.util.AbstractList class which forms the base class for ArrayList

Upvotes: 7

Related Questions