RxJava Operator Series 2 (Part 1)

RxJava Operator Series 2 (Part 1)

[[180035]]

RxJava operator source code portal

  • https://github.com/xiehui999/fuseProgram

In the previous article, RxJava Operator Series 1, we introduced operators that create observers. Today's article will introduce the frequently used conversion operators.

Map

This operator applies a function to each item emitted by the original Observable and returns an Observable that emits those results.

For example, if we have an integer array of data, when it is greater than 5, the output is true, then the code implementation

  1. Integer [] integers = {0, 9, 6, 4, 8};
  2. Observable. from (integers).map(new Func1() {
  3. @Override
  4. public Boolean call( Integer   integer ) {
  5. Log.e(TAG, "call: " + integer );
  6. return ( integer > 5 );
  7. }
  8. }).subscribe(new Subscriber() {
  9. @Override
  10. public void onCompleted() {
  11. Log.e(TAG, "onCompleted: " );
  12. }
  13.   
  14. @Override
  15. public void onError(Throwable e) {
  16. Log.e(TAG, "onError: " );
  17. }
  18.   
  19. @Override
  20. public void onNext(Boolean aBoolean) {
  21. Log.e(TAG, "onNext: " +aBoolean);
  22. }
  23. });

Log output information

  1. call: 0
  2. onNext: false  
  3. call: 9
  4. onNext: true  
  5. call: 6
  6. onNext: true  
  7. call: 4
  8. onNext: false  
  9. call: 8
  10. onNext: true  
  11. onCompleted:

For map, it can transform the data source into the type you want. For example, if you want to get a Student object (with age and name attributes), we can get only the name through map. Next, let's take another example. We get an image according to an image path and set the image to ImageView, and then add ImageView to our layout.

  1. String path = Environment.getExternalStorageDirectory()+ File.separator+ "aaa.jpg" ;
  2. Observable.just(path)
  3. .subscribeOn(Schedulers.io())
  4. .map(new Func1() {
  5. @Override
  6. public Bitmap call(String s) {
  7. Bitmap bitmap = BitmapFactory.decodeFile(s);
  8. Log.e(TAG, "call: Bitmap" +bitmap);
  9. return bitmap;
  10. }
  11. }).map(new Func1() {
  12. @Override
  13. public ImageView call(Bitmap bitmap) {
  14. Log.e(TAG, "call: ImageView" );
  15. ImageView imageView = new ImageView(getActivity());
  16. LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  17. imageView.setLayoutParams(params);
  18. imageView.setImageBitmap(bitmap);
  19. return imageView;
  20. }
  21. }).observeOn(AndroidSchedulers.mainThread())
  22. .subscribe(new Subscriber() {
  23. @Override
  24. public void onCompleted() {
  25. Log.e(TAG, "onCompleted: " );
  26. }
  27. @Override
  28. public void onError(Throwable e) {
  29. Log.e(TAG, "onError: " );
  30. }
  31.   
  32. @Override
  33. public void onNext(ImageView imageView) {
  34. Log.e(TAG, "onNext: " );
  35. layout.addView(imageView);
  36. }
  37. });

Cast

This operator is used to perform some forced type conversion operations. For example, when we jump to a page, the data object is often serialized. When we receive the data on the new page, we must force it to be converted to the type we want. The cast operator can also achieve this function. As follows

  1. Observable.just( serializable ) .cast (FileInfo.class).subscribe(new Subscriber() {
  2. @Override
  3. public void onCompleted() {
  4. Log.e(TAG, "onCompleted: " );
  5. }
  6.   
  7. @Override
  8. public void onError(Throwable e) {
  9. Log.e(TAG, "onError: " );
  10. }
  11.   
  12. @Override
  13. public void onNext(FileInfo fileInfo) {
  14. Log.e(TAG, "onNext: " +fileInfo.toString());
  15. tv1.append( "\n" +fileInfo.toString());
  16. }
  17. });

However, the actual use of this operator is not so widespread and is rarely used. Of course, this operator can also achieve the same role as instanceof in Java, which is used for type checking. When it is not of that type, the onError() method will be executed.

FlatMap

The difference between this operator and the map operator is that it transforms an Observable that emits data into multiple Observables, and then merges the data they emit into a single Observable.

  1. Integer [] integers = {1, 2, 3};
  2. Observable. from (integers).flatMap(new Func1>() {
  3. @Override
  4. public Observable call(final Integer   integer ) {
  5. return Observable.create (new Observable.OnSubscribe () {
  6. @Override
  7. public void call(Subscriber super String> subscriber) {
  8. Log.e(TAG, "call: FlatMap " + Thread.currentThread().getName());
  9. try {
  10. Thread.sleep(200);
  11. subscriber.onNext( integer + 100 + " FlatMap" );
  12. subscriber.onCompleted();
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. subscriber.onError(e);
  16. }
  17. }
  18. }).subscribeOn(Schedulers.newThread());
  19. }
  20. }).observeOn(AndroidSchedulers.mainThread())
  21. .subscribe(new Subscriber() {
  22. @Override
  23. public void onCompleted() {
  24. Log.e(TAG, "onCompleted: FlatMap" );
  25. }
  26.   
  27. @Override
  28. public void onError(Throwable e) {
  29. Log.e(TAG, "onError: FlatMap" );
  30. }
  31.   
  32. @Override
  33. public void onNext(String s) {
  34. Log.e(TAG, "onNext: FlatMap " + s);
  35. }
  36. });

Print log information

  1. call: FlatMap RxNewThreadScheduler-2
  2. call: FlatMap RxNewThreadScheduler-3
  3. call: FlatMap RxNewThreadScheduler-4
  4. onNext: FlatMap 101 FlatMap
  5. onNext: FlatMap 102 FlatMap
  6. onNext: FlatMap 103 FlatMap
  7. onCompleted: FlatMap

ConcatMap

This operator is similar to the simplest version of flatMap, but it concatenates the resulting Observables in order instead of merging them, and then produces its own sequence of data. Change the above flatMap code to the following

  1. Integer [] integers = {1, 2, 3};
  2. Observable. from (integers).concatMap(new Func1>() {
  3. @Override
  4. public Observable call(final Integer   integer ) {
  5. return Observable.create (new Observable.OnSubscribe () {
  6. @Override
  7. public void call(Subscriber super String> subscriber) {
  8. Log.e(TAG, "call:2 ConcatMap " + Thread.currentThread().getName());
  9. try {
  10. Thread.sleep(200);
  11. subscriber.onNext( integer + 100 + " ConcatMap" );
  12. subscriber.onCompleted();
  13. } catch (InterruptedException e) {
  14. e.printStackTrace();
  15. subscriber.onError(e);
  16. }
  17. }
  18. }).subscribeOn(Schedulers.newThread());
  19. }
  20. }).observeOn(AndroidSchedulers.mainThread())
  21. .subscribe(new Subscriber() {
  22. @Override
  23. public void onCompleted() {
  24. Log.e(TAG, "onCompleted: ConcatMap" );
  25. }
  26.   
  27. @Override
  28. public void onError(Throwable e) {
  29. Log.e(TAG, "onError: ConcatMap" );
  30. }
  31.   
  32. @Override
  33. public void onNext(String s) {
  34. Log.e(TAG, "onNext: ConcatMap " +s);
  35. }
  36. });

Continue

<<:  Dependency Injection on Android Platform (I)

>>:  A comprehensive summary of Android adaptation problems

Recommend

Best Practices for Selecting Software Composition Analysis Tools

Software composition analysis (SCA) tools were cr...

What should a super TV look like in users’ minds?

The trend of "Internet +" is continuous...

How to build a user operation system?

Whether it is offline training or online classes,...

Does Ocpc have a decline period? How to optimize?

After I published several Q&A articles about ...

Blind box marketing is a double-edged sword!

In the past two years, the blind box economy has ...

Xiaohongshu’s complete promotion and operation plan!

At the beginning, my purpose of operating Xiaohon...

7 essential skills for operations - marketing promotion steps!

A correct marketing promotion process Marketing pr...

How to operate Douyin Store Alliance? How to join the Douyin Store Alliance?

Everyone should be playing Douyin, and there are ...

iOS 9's new features will target Google's search business

There have been a lot of reports about Apple addi...

How to achieve "real sales" KOL marketing in 2 steps?

Unilever CMO Keith Weed announced at the recent C...