Get size of an Iterable in Java

前端 未结 10 1220
半阙折子戏
半阙折子戏 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:12

    This is perhaps a bit late, but may help someone. I come across similar issue with Iterable in my codebase and solution was to use for each without explicitly calling values.iterator();.

    int size = 0;
    for(T value : values) {
       size++;
    }
    

提交回复
热议问题