What is the best way to get the count/length/size of an iterator?

后端 未结 9 1117
青春惊慌失措
青春惊慌失措 2020-12-03 00:01

Is there a \"computationally\" quick way to get the count of an iterator?

int i = 0;
for ( ; some_iterator.hasNext() ; ++i ) some_iterator.next();

9条回答
  •  醉话见心
    2020-12-03 00:43

    Using Guava library:

    int size = Iterators.size(iterator);
    

    Internally it just iterates over all elements so its just for convenience.

提交回复
热议问题