Get size of an Iterable in Java

前端 未结 10 1212
半阙折子戏
半阙折子戏 2020-12-08 03:48

I need to figure out the number of elements in an Iterable in Java. I know I can do this:

Iterable values = ...
it = values.iterator();
while (i         


        
10条回答
  •  無奈伤痛
    2020-12-08 04:14

    I would go for it.next() for the simple reason that next() is guaranteed to be implemented, while remove() is an optional operation.

    E next()
    

    Returns the next element in the iteration.

    void remove()
    

    Removes from the underlying collection the last element returned by the iterator (optional operation).

提交回复
热议问题