@Override public void run() { Observable.range(1, 10).takeFirst(new Func1<Integer, Boolean>() { @Override public Boolean call(Integer integer) { return integer % 2 == 0; } }).subscribe(new Action1<Integer>() { @Override public void call(Integer integer) { log(integer); } }); } });
/** * Returns an Observable that emits only the very first item emitted by the source Observable that satisfies * a specified condition, or notifies of an {@code NoSuchElementException} if no such items are emitted. * <p> * <img width="640" height="310" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/firstN.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code first} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param predicate * the condition that an item emitted by the source Observable has to satisfy * @return an Observable that emits only the very first item emitted by the source Observable that satisfies * the {@code predicate}, or raises an {@code NoSuchElementException} if no such items are emitted * @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#first-and-takefirst">RxJava wiki: takeFirst</a> * @see "MSDN: Observable.firstAsync" */ public final Observable<T> first(Func1<? super T, Boolean> predicate) { return takeFirst(predicate).single(); }
/** * Returns an Observable that emits only the very first item emitted by the source Observable that satisfies * a specified condition, or a default item if the source Observable emits no such items. * <p> * <img width="640" height="310" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/firstOrDefaultN.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code firstOrDefault} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param predicate * the condition any item emitted by the source Observable has to satisfy * @param defaultValue * the default item to emit if the source Observable doesn't emit anything that satisfies the * {@code predicate} * @return an Observable that emits only the very first item emitted by the source Observable that satisfies * the {@code predicate}, or a default item if the source Observable emits no such items * @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#firstordefault">RxJava wiki: firstOrDefault</a> * @see "MSDN: Observable.firstOrDefaultAsync" */ public final Observable<T> firstOrDefault(T defaultValue, Func1<? super T, Boolean> predicate) { return takeFirst(predicate).singleOrDefault(defaultValue); }
.takeFirst(new Func1<String, Boolean>() { @Override public Boolean call(String s) {