You need to go through the iterator and count the elements. For example,
while (iterator.hasNext()) { iterator.next(); count++; }
This is not very clean, but the iterator is used to iterate and does not provide any specific api for this.
But where is the iterator created? If this comes from another object, such as a Collection object, you can determine the size of the source, not the iterator itself.
Safime
source share