Java Iterator: What does it mean :java.util.ArrayList$Itr

Viewed 1308

There is code :

List<Integer> al = new ArrayList<>() ;
Iterator iterator = al.iterator();
System.out.println(iterator.getClass().getName());

I get :

 java.util.ArrayList$Itr

What does it mean: "ArrayList$Itr" ?

What implementation of Iterator i get in this line ?

Iterator iterator = al.iterator(); 
3 Answers

Inside the ArrayList class, the following inner class is defined:

private class Itr implements Iterator<E> {...}

al.iterator() returns an instance of that class, whose full name is java.util.ArrayList$Itr.

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

java.util.ArrayList has internal private class Itr implements Iterator<E> defined inside ArrayList which is returned when you call list.iterator() method.

Refer internal implementation of java.util.ArrayList.

What does it mean: "ArrayList$Itr" ?

This means Itr is an inner class of ArrayList.

What implementation of Iterator i get in this line?

The implementation you will get is (as of now) java.util.ArrayList$Itr. But you should not rely on this specific implementation as this is not part of the API of ArrayList and might change in future versions of Java. The only thing you can rely on is that you will get an Iterator.

Related