观察者模式(一)

//window操作符
     private static void windowOperator() {
    Observable.just(78,88,9,845,21,5,6,8)
    .window(3)
    .subscribe(new Consumer<Observable>() {
    @Override
    public void accept(Observable integerObservable) throws Exception {
    System.out.println(“On next”);
    integerObservable.subscribe(new Consumer() {
    @Override
    public void accept(Integer integer) throws Exception {
    System.out.println(“accept”+integer);
    }
    });
    }
    });
 //发送第N项数据
 //elementAt 自发送第N项数据
//elementAtOperator();

private static void elementAtOperator() {
    Observable.just(1,5,7,8,9,52,68)
            .elementAt(8,78)
            .subscribe(new Consumer<Integer>() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(integer+"第N项数据");
                }
            });


//ignoreElements 不发送任何数据
//ignoreElemtents();
private static void ignoreElemtents() {
    Observable.just(102,3,34,42,3)
            .ignoreElements()
            .subscribe(new Action() {
                @Override
                public void run() throws Exception {
                    System.out.println("空数据发送成功!");
                }
            });
}


    //flatMapOpretor操作符
//flatMapOpretor();
private static void flatMapOpretor() {
    User user = new User();
    user.username = "张三";
    user.addresses = new ArrayList<>();
    User.Address address = new User.Address();
    address.city = "北京";
    address.street = "朝阳北路";
   user.addresses.add(address);

   //groupByOpretor操作符
	//groupByOperator();
   
    private static void groupByOperator() {
    Observable.range(1,8)
            .groupBy(new Function<Integer, String>() {
                @Override
                public String apply(Integer integer) throws Exception {
                    return (integer%2==0?"偶数":"奇数");
                }
            })
            .subscribe(new Consumer<GroupedObservable<String, Integer>>() {
                @Override
                public void accept(final GroupedObservable<String, Integer> stringIntegerGroupedObservable) throws Exception {
                    if(stringIntegerGroupedObservable.getKey().equals("奇数")){
                        stringIntegerGroupedObservable.subscribe(new Consumer<Integer>() {
                            @Override
                            public void accept(Integer integer) throws Exception {
                                System.out.println(stringIntegerGroupedObservable.getKey()+integer);
                            }
                        });
                    }
                }
            });
            }
  //filter操作符
      private static void filterOperator() {
    Observable.just(1,3,5,6,3,64,3,2,53,5)
            .filter(new Predicate<Integer>() {
                @Override
                public boolean test(Integer integer) throws Exception {
                    return integer!=-1;
                }
            }).subscribe(new Consumer<Integer>() {
        @Override
        public void accept(Integer integer) throws Exception {
            System.out.println(integer+"");
        }
    });

//distinct过滤掉重复的数据项
//distinctOperator();
private static void distinctOperator() {
    Observable.just(2,3,4,2,3,4)
            .distinct()
            .subscribe(new Consumer<Integer>() {
                @Override
                public void accept(Integer integer) throws Exception {
                    System.out.println(integer+"过滤");
                }
            });

//map操作符
mapOpretor();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值