Java Code Examples for io.reactivex.Observable.filter()

The following are Jave code examples for showing how to use filter() of the io.reactivex.Observable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Learning-RxJava   File: Ch2_3.java   View Source Code Vote up 6 votes
public static void main(String[] args) {
    Observable<String> source = Observable.create(emitter -> {
        try {
            emitter.onNext("Alpha");
            emitter.onNext("Beta");
            emitter.onNext("Gamma");
            emitter.onNext("Delta");
            emitter.onNext("Epsilon");
            emitter.onComplete();
        } catch (Throwable e) {
            emitter.onError(e);
        }
    });
    Observable<Integer> lengths = source.map(String::length);
    Observable<Integer> filtered = lengths.filter(i -> i >= 5);
    filtered.subscribe(s -> System.out.println("RECEIVED: " +
            s));
}
 
Example 2
Project: RxLifeCycle   File: RxLifecycle.java   View Source Code Vote up 5 votes
private static <R> Observable<R> takeUntilEvent(final Observable<R> lifecycle, final R event) {
    return lifecycle.filter(new Predicate<R>() {
        @Override
        public boolean test(R lifecycleEvent) throws Exception {
            return lifecycleEvent.equals(event);
        }
    });
}
 
Example 3
Project: buckaroo   File: SummaryView.java   View Source Code Vote up 5 votes
public static Observable<Component> render(final Observable<Event> events) {

        Preconditions.checkNotNull(events);

        final Observable<Event> fileModifiedEvent = events
            .filter(x -> x instanceof WriteFileEvent || x instanceof TouchFileEvent);

        final Observable<ImmutableList<Component>> modifiedSummary = fileModifiedEvent.scan(
            ImmutableList.of(),
            (ImmutableList<Event> list, Event event) -> Streams
                .concat(list.stream(), Stream.of(event))
                .collect(toImmutableList()))
            .map((ImmutableList<Event> modifiedFiles) -> modifiedFiles.isEmpty() ?
                ImmutableList.of() :
                ImmutableList.of(
                        Text.of("Files modified (" + modifiedFiles.size() + "): "),
                            ListLayout.of(
                                modifiedFiles.stream()
                                    .map(GenericEventRenderer::render)
                                    .collect(toImmutableList()))));

        final Observable<ImmutableList<Component>> resolvedDependencies = events
            .ofType(ResolvedDependenciesEvent.class)
            .map(GenericEventRenderer::render)
            .startWith(StackLayout.of())
            .map(ImmutableList::of);


        final Observable<ImmutableList<Component>> notifications = events
            .ofType(Notification.class)
            .map(GenericEventRenderer::render)
            .scan(ImmutableList.of(), MoreLists::append);

        return Observable.combineLatest(
            modifiedSummary, resolvedDependencies, notifications, MoreLists::concat)
            .map(StackLayout::of)
            .cast(Component.class);
    }