Rxjava2.0メモ-005-判断オペレータ

3122 ワード

all

 /**
     *    
     */
    private void useAll() {

        Observable.just(1, 2, 3, 4, 5)
                .all(new Predicate() {
                    @Override
                    public boolean test(Integer integer) throws Exception {
                        return integer <= 10;
                    }
                }).subscribe(new Consumer() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {

                KLog.d(TTAG, " " + aBoolean);
            }
        });
    }

TakeWhile

 // 1.  1s 1  =  0 , 1, 0、1、2、3
        Observable.interval(1, TimeUnit.SECONDS)
                // 2.  takeWhile 
                .takeWhile(new Predicate() {
                    @Override
                    public boolean test(Long integer) throws Exception {
                        return (integer < 3);
                        //  <3 , Observable 
                    }
                }).subscribe(new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(Long value) {
                KLog.d(TTAG, "  " + value);
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });


skipWhile

// 1.  1s 1  =  0 , 1
        Observable.interval(1, TimeUnit.SECONDS)
                // 2.  skipWhile() 
                .skipWhile(new Predicate() {
                    @Override
                    public boolean test(Long aLong) throws Exception {
                        return (aLong < 5);
                        //   = false =  ≥5, 
                    }
                }).subscribe(new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(Long value) {
                KLog.d(TTAG, "  " + value);
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onComplete() {
            }
        });

sequenceEqual

 Observable.sequenceEqual(
                Observable.just(4, 5, 6),
                Observable.just(4, 5, 6)
        )
                .subscribe(aBoolean -> {
                    KLog.d(TTAG, "2 Observable :" + aBoolean);
                    //  
                });

contains

 /**
     *  
     * 

* , true; , false * = exists() */ private void contains() { Observable.just(1, 2, 3, 4, 5, 6) .contains(4) .subscribe(aBoolean -> { KLog.d(TTAG, "result is " + aBoolean); // }); }