getComponents will be called once and will return an Iterator. Then this iterator will be called using the next () and hasNext () methods.
Refresh Here is a little more detail to try and release Skeet Jon Skeet on this answer.
The following program shows how to call iterator once, even if there are three elements in the collection.
public static void main(String[] args) { List<String> list = new ArrayList<String>() { public java.util.Iterator<String> iterator() { System.out.println("iterator() called"); return super.iterator(); }; }; list.add("a"); list.add("b"); list.add("c"); for (String s : list) { System.out.println(s); } }
Output:
iterator() called a b c
If you run this through a Java decompiler, you will find the following code:
String s; for(Iterator iterator = list.iterator(); iterator.hasNext(); System.out.println(s)) s = (String)iterator.next();
Add, since we know from JLS 14.14.1 that the first section of the for statement is executed only once, we can rest assured that the iterator method will not be called several times.
source share