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)
| 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 |