MCPcopy
hub / github.com/google/guava / tryFind

Method tryFind

guava/src/com/google/common/collect/Iterators.java:775–785  ·  view source on GitHub ↗

Returns an {@link Optional} containing the first element in {@code iterator} that satisfies the given predicate, if such an element exists. If no such element is found, an empty {@link Optional} will be returned from this method and the iterator will be left exhausted: its {@code hasNext()} method w

(Iterator<T> iterator, Predicate<? super T> predicate)

Source from the content-addressed store, hash-verified

773 * @since 11.0
774 */
775 public static <T> Optional<T> tryFind(Iterator<T> iterator, Predicate<? super T> predicate) {
776 checkNotNull(iterator);
777 checkNotNull(predicate);
778 while (iterator.hasNext()) {
779 T t = iterator.next();
780 if (predicate.apply(t)) {
781 return Optional.of(t);
782 }
783 }
784 return Optional.absent();
785 }
786
787 /**
788 * Returns the index in {@code iterator} of the first element that satisfies the provided {@code

Callers 1

tryFindMethod · 0.95

Calls 6

ofMethod · 0.95
absentMethod · 0.95
nextMethod · 0.65
applyMethod · 0.65
checkNotNullMethod · 0.45
hasNextMethod · 0.45

Tested by

no test coverage detected