Rxjava实践之路[初级篇]

AurPye 8年前
   <h2><strong>前言</strong></h2>    <p>本章主要讲过滤,如何从发射的Observable中选取想要的内容以及学会更多有用的处理技巧。</p>    <h3><strong>正题</strong></h3>    <p><strong>1、filter</strong></p>    <ul>     <li> <p>筛选符合条件的内容</p> <pre>  <code class="language-java">/**     * 筛选符合条件的内容     * @author leibing     * @createTime 2016/09/18     * @lastModify 2016/09/18     * @param     * @return     */    private void filterObservable(){        // 使用filter方法过滤内容中不需要的值        Observable.just("A1","B1","A2","C3").filter(new Func1<String, Boolean>() {            @Override            public Boolean call(String s) {                // 条件筛选以“A”开始的内容                return s.startsWith("A");            }        }).subscribe(new Action1<String>() {            @Override            public void call(String s) {                // 打印结果                System.out.println("ddddddddddddddddddd filter s = " + s);            }        });    }</code></pre> 运行结果如下: <pre>  <code class="language-java">09-18 14:32:29.978 25356-25356/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddddd filter s = A1  09-18 14:32:29.978 25356-25356/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddddd filter s = A2</code></pre> <p><strong>2、take</strong></p> </li>     <li> <p>筛选出原始数据中前n个数据发射</p> <pre>  <code class="language-java">/**     * 筛选出原始数据中前n个数据发射     * @author leibing     * @createTime 2016/09/18     * @lastModify 2016/09/18     * @param     * @return     */    private void takeObservable(){        // take(n) 提取原数据前n个数据发射        Observable.just("1","2","3","4","5").take(4).subscribe(new Action1<String>() {            @Override            public void call(String s) {                // 打印结果                System.out.println("ddddddddddddddd take s = " + s);            }        });    }</code></pre> <p>运行结果如下:</p> <pre>  <code class="language-java">09-18 14:46:21.894 424-424/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddd take s = 1  09-18 14:46:21.894 424-424/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddd take s = 2  09-18 14:46:21.894 424-424/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddd take s = 3  09-18 14:46:21.895 424-424/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddd take s = 4</code></pre> <p><strong>3、takelast</strong></p> </li>     <li> <p>筛选出原始数据中从倒数前n个数据发射</p> <pre>  <code class="language-java">/**     * 筛选出原始数据中从倒数前n个数据发射     * @author leibing     * @createTime 2016/09/18     * @lastModify 2016/09/18     * @param     * @return     */    private void takeLastObservable(){        // takeLast(n) 提取原数据倒数前n个数据发射        Observable.just("1","2","3","4","5").takeLast(2).subscribe(new Action1<String>() {            @Override            public void call(String s) {                // 打印结果                System.out.println("dddddddddddddddddd takeLast s = " + s);            }        });    }</code></pre> </li>    </ul>    <p><strong>4、distinct</strong></p>    <ul>     <li> <p>去掉数据中重复值</p> <pre>  <code class="language-java">/**     * 去掉数据中重复值     * @author leibing     * @createTime 2016/09/18     * @lastModify 2016/09/18     * @param     * @return     */    private void distinctObservable(){        // 将发射数据重复五遍再去重复        Observable.just("A1","A1","B1","C1","D1","B1","A1").repeat(5).distinct().subscribe(new Action1<String>() {            @Override            public void call(String s) {                // 打印结果                System.out.println("ddddddddddddddddd distinct s = " + s);            }        });    }</code></pre> 运行结果如下: <pre>  <code class="language-java">09-18 14:54:35.616 6187-6187/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddd distinct s = A1  09-18 14:54:35.616 6187-6187/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddd distinct s = B1  09-18 14:54:35.617 6187-6187/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddd distinct s = C1  09-18 14:54:35.617 6187-6187/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddddd distinct s = D1</code></pre> <p><strong>5、skip</strong></p> </li>     <li> <p>筛选出原始数据中前n个数据跳过不发射</p> <pre>  <code class="language-java">/**     * 筛选出原始数据中前n个数据跳过不发射     * @author leibing     * @createTime 2016/09/18     * @lastModify 2016/09/18     * @param     * @return     */    private void skipObservable(){        // skip(n) 提取原数据前n个数据跳过不发射        Observable.just("1","2","3","4","5").skip(4).subscribe(new Action1<String>() {            @Override            public void call(String s) {                // 打印结果                System.out.println("ddddddddddddddd skip s = " + s);            }        });    }</code></pre> 运行结果如下: <pre>  <code class="language-java">09-18 15:04:10.187 11687-11687/cn.jianke.rxjavafilterdemo I/System.out: ddddddddddddddd skip s = 5</code></pre> </li>    </ul>    <p><strong>6、skiplast</strong></p>    <ul>     <li> <p>筛选出原始数据中从倒数前n个数据跳过不发射</p> <pre>  <code class="language-java">/**     * 筛选出原始数据中从倒数前n个数据跳过不发射     * @author leibing     * @createTime 2016/09/18     * @lastModify 2016/09/18     * @param     * @return     */    private void skipLastObservable(){        // takeLast(n) 提取原数据倒数前n个数据跳过不发射        Observable.just("1","2","3","4","5").skipLast(2).subscribe(new Action1<String>() {            @Override            public void call(String s) {                // 打印结果                System.out.println("dddddddddddddddddd skipLast s = " + s);            }        });    }</code></pre> 运行结果如下: <pre>  <code class="language-java">09-18 15:05:31.576 11687-11687/cn.jianke.rxjavafilterdemo I/System.out: dddddddddddddddddd skipLast s = 1  09-18 15:05:31.577 11687-11687/cn.jianke.rxjavafilterdemo I/System.out: dddddddddddddddddd skipLast s = 2  09-18 15:05:31.578 11687-11687/cn.jianke.rxjavafilterdemo I/System.out: dddddddddddddddddd skipLast s = 3</code></pre> <p><strong>7、timeout</strong></p> </li>     <li> <p>在指定的时间间隔内Observable不发射值的话,监听的原始的Observable时就会触发onError()函数</p> <pre>  <code class="language-java">/**     * 在指定的时间间隔内Observable不发射值的话,监听的原始的Observable时就会触发onError()函数     * @author leibing     * @createTime 2016/09/18     * @lastModify 2016/09/18     * @param     * @return     */    private void timeoutObservable(){        // 指定4秒后再发射,再指定3秒后如果未发射就触发onError函数        Observable.just("1","2").timer(4, TimeUnit.SECONDS).timeout(3, TimeUnit.SECONDS).subscribe(                new Observer<Long>() {            @Override            public void onCompleted() {                System.out.println("dddddddddddddddd timeout onCompleted = ");            }              @Override            public void onError(Throwable e) {                System.out.println("dddddddddddddddd timeout e = " + e.getMessage());            }              @Override            public void onNext(Long aLong) {                System.out.println("dddddddddddddddd timeout along = " + aLong);            }        });    }</code></pre> <p>运行结果如下:</p> <pre>  <code class="language-java">09-18 15:28:34.142 25661-25812/cn.jianke.rxjavafilterdemo I/System.out: dddddddddddddddd timeout e = null</code></pre> </li>    </ul>    <p>demo地址: <a href="/misc/goto?guid=4959716108668670282" rel="nofollow,noindex">RxjavaFilterDemo</a></p>    <p> </p>    <p> </p>    <p>来自:http://www.jianshu.com/p/a7b2142ceffa</p>    <p> </p>