Return type of iterator() method in java

20,825

Solution 1

I want to know what is here the return type of al.iterator()

You shouldn't care. It doesn't matter what particular implementation of Iterator the method returns, only that it returns an Iterator. You can call next, hasNext, and remove, or iterate with a for-each loop, without knowing that it happens to be an ArrayList.Itr or whatever the implementation is.

Suppose the Java developers working on the ArrayList want to rename their iterator implementation to ArrayListIterator instead of whatever they're calling it now. If your code relied on knowing the exact Iterator implementation, you would have to change your code just to handle an ArrayList-internal change you shouldn't even see.

Solution 2

You are right saying that Iterator is an interface. So, some class must implement this interface so you can use it.

The iterator interface defines what an iterator should do. In this case, the class ArrayList provides its own implementation of this interface.

What you get from al.iterator() is the inner class of ArrayList Itr (see following code, which IS iterator. (as we kind of describe the IS-A relationship)

(you can think of an inner class as a normal class for now. So the answer to your question is Itr, which is just a name, and all you should care about is its function, which is defined by interface Iterator.)

   /**
     * An optimized version of AbstractList.Itr
     */
    private class Itr implements Iterator<E> {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        public boolean hasNext() {
            return cursor != size;
        }
       .....
  }

This code is by Josh Bloch and Neal Gafter, who wrote the ArrayList class.

Solution 3

You can define the return type of your Iterator beforehand. As you declare the iterator, you can (and should) define the type of your list items like this:

Iterator<File> itr = al.iterator();

So when you access itr you don't have to cast to file, e.g. (File) itr.next() but can directly use it in your while loop:

while (it.hasNext()) { File f = itr.next() }

Your compiler now won't complain when using next().

Solution 4

The implementation is defined in AbstractList itself. Find it at its source code.

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
    private class Itr implements Iterator<E> {
       ...
    }

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

Try

System.out.println(new ArrayList<String>().iterator().getClass().getName());

output

java.util.AbstractList$Itr
Share:
20,825
user3528086
Author by

user3528086

Updated on November 15, 2020

Comments

  • user3528086
    user3528086 over 3 years

    I am a new-comer in Java and in process of learning. I need a an answer of following question supported with valid theory. Consider the following line-

    Iterator itr = al.iterator(); 
    

    where al is some collection object of ArrayList (class) type. I want to know what is here the return type of

    al.iterator()

    It is not definitely of a primitive data type, then it could be an object , but since every object belong to a class then it is of which class. Documentation and books etc says it has return type of Iterator. But Iterator is an interface. On other hand we say an interface could not have a direct object. Although interface variable could refer to an object of a class or classes that implements it.

    -

    So the above syntax is correct (as Iterator variable itr could be used to refer to an object of some class implementing it). But in realty it is object of which class? And can substituting itr with reference variable of that class will cause no error (I have tried substituting itr with ref. variable of ArrayList class in above line but that causes an error). I am using this syntax very often also in Generics form, but dont know the theory behind this. And I am supposing I am lacking a very basic concept here. Please rectify.