Looking through the java source code, I faced with incomprehensible for me construction in the hashCode() method of the class AbstractList. This is implementation of the hashCode method for ArrayList. I don't understand how it iterates with for-each.
public int hashCode() {
int hashCode = 1;
for (E e : this) //<--???
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
return hashCode;
}
E is the type of the element. But to which class(type) pointer this belongs?
But to which class(type) pointer this belongs?
this
is the list that hashCode
was called on. So the compile-time type is AbstractList<E>
.
It's saying "for every element in this list, include that element's hash code in the result".
See more on this question at Stackoverflow