当前位置:   article > 正文

Android响应式编程开发RxAndroid(4):flatMap_android flatmap

android flatmap

Android响应式编程开发RxAndroid(4):flatMap

RxAndroid的map通过Function把一个对象转换为另外一个对象,作为结果输出。flatMap则可以通俗的理解为把一个集合元素对象拆分成单个元素对象,然后丢给Observer作为输入。例子:

  1. package zhangphil.app;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import java.util.Arrays;
  6. import java.util.List;
  7. import io.reactivex.Observable;
  8. import io.reactivex.android.schedulers.AndroidSchedulers;
  9. import io.reactivex.functions.Function;
  10. import io.reactivex.observers.DisposableObserver;
  11. import io.reactivex.schedulers.Schedulers;
  12. public class MainActivity extends Activity {
  13. //假设一个数据源
  14. private String TAG = "zhangphil测试";
  15. private DisposableObserver<String> observer = new DisposableObserver<String>() {
  16. @Override
  17. public void onNext(String s) {
  18. Log.d(TAG, "onNext - Start");
  19. Log.d(TAG, String.valueOf(s));
  20. Log.d(TAG, "onNext - End");
  21. }
  22. @Override
  23. public void onComplete() {
  24. Log.d(TAG, "onComplete");
  25. }
  26. @Override
  27. public void onError(Throwable e) {
  28. Log.e(TAG, e.toString(), e);
  29. }
  30. };
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. String[] source = {"zhang", "phil", "@", "csdn"};
  35. List<String> lists = Arrays.asList(source);
  36. Observable
  37. .just(lists)
  38. .subscribeOn(Schedulers.io())//执行任务的线程
  39. .observeOn(AndroidSchedulers.mainThread())//回调发生的线程
  40. .flatMap(function)
  41. .subscribe(observer);
  42. }
  43. // 设置映射函数
  44. private Function<List<String>, Observable<String>> function = new Function<List<String>, Observable<String>>() {
  45. @Override
  46. public Observable<String> apply(List<String> lists) {
  47. String[] strs = new String[lists.size()];
  48. for (int i = 0; i < lists.size(); i++) {
  49. strs[i] = lists.get(i).toUpperCase();
  50. }
  51. return Observable.fromArray(strs);
  52. }
  53. };
  54. }


代码运行结果输出:

03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: onNext - Start
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: ZHANG
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: onNext - End
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: onNext - Start
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: PHIL
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: onNext - End
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: onNext - Start
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: @
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: onNext - End
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: onNext - Start
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: CSDN
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: onNext - End
03-02 10:31:06.606 18037-18037/zhangphil.app D/zhangphil测试: onComplete


附录:
1,《Android响应式编程RxAndroid(1):简单例程》链接地址:http://blog.csdn.net/zhangphil/article/details/58593782  
2,《Android响应式编程开发RxAndroid(2):简单例程的Android改造 》链接地址:http://blog.csdn.net/zhangphil/article/details/58597372
3,《Android响应式编程开发RxAndroid(3):map》链接地址:http://blog.csdn.net/zhangphil/article/details/59130641

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/177417
推荐阅读
相关标签
  

闽ICP备14008679号