RxJava2.0----布尔操作符Boolean Operat

2017-11-19  本文已影响24人  Calllanna

5.布尔操作符Boolean Operators

Operators that evaluate one or more Observables or items emitted by Observables
● All
● AmbWith
● any
● Contains
● DefaultIfEmpty
● sequenceEqual

● All
判断所有数据是否满足一定条件

Observable.fromArray(new String[]{"aaa","abb","ac","ad"})
                .any(new Predicate<String>() {
                    @Override
                    public boolean test(String s) throws Exception {
                        return s.contains("a");
                    }
                }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                print("any accept:"+aBoolean);//true
            }
        });

● Amb
获取两个事件流最先发数据的一个,Amb操作符有方法ambArray(),ambWith(),

  Observable.ambArray(
                Observable.intervalRange(100,10,300,0, TimeUnit.MILLISECONDS),
                Observable.intervalRange(0,10,0,0, TimeUnit.MILLISECONDS)).subscribe(
                new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        print("1 amb accpet :"+aLong);//0,1,2,3,4,5,6,7,8,9
                    }
                }
        );
        Observable.intervalRange(100,10,0,0, TimeUnit.MILLISECONDS)
                .ambWith(Observable.intervalRange(0,10,1000,0, TimeUnit.MILLISECONDS))
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        print( "2 amb accpet :"+aLong);//100,101,102,103,104,105,106,107,108,109
                    }
                });

● any
● Contains

判断所有数据的其中一个是否满足一定条件

    Observable.fromArray(new String[]{"aaa","abb","ac","ad"})
                .any(new Predicate<String>() {
                    @Override
                    public boolean test(String s) throws Exception {
                        return s.contains("e");
                    }
                }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                print("any accept:"+aBoolean);//false
            }
        });
    Observable.fromArray(new String[]{"aaa","abb","ac","ad"})
                .contains("aaa").subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                print("contains accept:"+aBoolean);//true
            }
        });

● DefaultIfEmpty
如果事件源没有数据最后,会发送默认的数据。

      Observable.fromArray(new String[]{})
               .defaultIfEmpty("--").subscribe(new Consumer<String>() {
           @Override
           public void accept(String s) throws Exception {
               print("defaultIfEmpty accept:"+s);//aaa,abb,ac,ad
           }
       });

● sequenceEqual
判断两个观察源事件流是否一致

 Observable.sequenceEqual( Observable.intervalRange(100,10,0,0, TimeUnit.MILLISECONDS),
                Observable.intervalRange(100,10,0,0, TimeUnit.MILLISECONDS)).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                print("sequenceEqual accept:"+aBoolean);//true
            }
        });
上一篇下一篇

猜你喜欢

热点阅读