1

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.

4
  • 2
    For example: grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/… Commented Sep 2, 2012 at 7:00
  • How are you searching? We have enough in JDK source code. Commented Sep 2, 2012 at 7:01
  • @jayan:i searched in a java.lang.util package the implementation classes Commented Sep 2, 2012 at 7:03
  • See this in OpenJDK as an example. Commented Sep 2, 2012 at 7:18

4 Answers 4

7

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

2 Comments

AbstractList has what you want. you can try.
it has two inner class describe the iterator.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.