Reputation: 25
Guava Iterators.any method documentation mentioned that this call will return one or more elements satisfy the predicate.Does it mean that the predicate run for all the elements in the iterator even though the first element satisfied the condition ?
Class Iterators
"Returns true if one or more elements returned by iterator satisfy the given predicate."
Upvotes: 0
Views: 303
Reputation: 14847
Iterators.any
source:
647 public static <T> boolean any(
648 Iterator<T> iterator, Predicate<? super T> predicate) {
649 checkNotNull(predicate);
650 while (iterator.hasNext()) {
651 T element = iterator.next();
652 if (predicate.apply(element)) {
653 return true;
654 }
655 }
656 return false;
657 }
It's a normal Iterator.
It will iterate until founds an element which is OK for the predicate. When it finish because the return true;
it means at least one element satisfied the condition but it could happen that others too satisfied the condition without the need to check. (that's the Returns true if one or more
part)
But if none satisfied the condition it will return false since noone stopped the iterator.
Upvotes: 3