1
下面是Iterables.isEmpty()
(ref)的源代碼:將Iterables投射到集合是否遍歷所有元素?
public static boolean isEmpty(Iterable<?> iterable) {
if (iterable instanceof Collection) {
return ((Collection<?>) iterable).isEmpty();
}
return !iterable.iterator().hasNext();
}
我在所有元素想知道如果Iterables.isEmpty()
方法迭代如果給iterable
參數是一種類型的Set
,其延伸Collection
。鑄造Iterable
到Collection
會導致完全迭代嗎?如果是這樣,爲什麼?如果不是,爲什麼不呢? Iterables.isEmpty()
is implemented as iterable.iterator().hasNext()
for non-Collection
s: