content: package com.rxjava2.android.samples.ui.operators;
  
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  
  import com.rxjava2.android.samples.R;
  import com.rxjava2.android.samples.utils.AppConstant;
  
  import java.util.Random;
  import java.util.concurrent.TimeUnit;
  
  import androidx.annotation.Nullable;
  import androidx.appcompat.app.AppCompatActivity;
  import io.reactivex.Observable;
  import io.reactivex.ObservableSource;
  import io.reactivex.Observer;
  import io.reactivex.android.schedulers.AndroidSchedulers;
  import io.reactivex.disposables.Disposable;
  import io.reactivex.functions.Function;
  import io.reactivex.schedulers.Schedulers;
  
  /**
   * Created by thanhtuan on 26/04/18.
   */
  public class SwitchMapExampleActivity extends AppCompatActivity {
  
      private static final String TAG = SwitchMapExampleActivity.class.getSimpleName();
      Button btn;
      TextView textView;
  
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_example);
  
          btn = findViewById(R.id.btn);
          textView = findViewById(R.id.textView);
  
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  doSomeWork();
              }
          });
      }
  
      /* whenever a new item is emitted by the source Observable, it will unsubscribe to and stop
       * mirroring the Observable that was generated from the previously-emitted item,
       * and begin only mirroring the current one.
       *
       * Result: 5x
       */
      private void doSomeWork() {
          getObservable()
                  .switchMap(new Function<Integer, ObservableSource<String>>() {
                      @Override
                      public ObservableSource<String> apply(Integer integer) {
                          int delay = new Random().nextInt(2);
  
                          return Observable.just(integer.toString() + "x")
                                  .delay(delay, TimeUnit.SECONDS, Schedulers.io());
                      }
                  })
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(getObserver());
      }
  
      private Observable<Integer> getObservable() {
          return Observable.just(1, 2, 3, 4, 5);
      }
  
      private Observer<String> getObserver() {
          return new Observer<String>() {
              @Override
              public void onSubscribe(Disposable d) {
                  Log.d(TAG, " onSubscribe : " + d.isDisposed());
              }
  
              @Override
              public void onNext(String value) {
                  textView.append(" onNext : value : " + value);
                  textView.append(AppConstant.LINE_SEPARATOR);
                  Log.d(TAG, " onNext value : " + value);
              }
  
              @Override
              public void onError(Throwable e) {
                  textView.append(" onError : " + e.getMessage());
                  textView.append(AppConstant.LINE_SEPARATOR);
                  Log.d(TAG, " onError : " + e.getMessage());
              }
  
              @Override
              public void onComplete() {
                  textView.append(" onComplete");
                  textView.append(AppConstant.LINE_SEPARATOR);
                  Log.d(TAG, " onComplete");
              }
          };
      }
  }
  