Map is used to transform upstream data and send it downstream.

Observable.create(new ObservableOnSubscribe<Integer>() { @Override public void subscribe(@NotNull ObservableEmitter<Integer> emitter) throws Exception { emitter.onNext(1); emitter.onNext(3); emitter.onNext(5); emitter.onComplete(); } }).map(new Function<Integer, Integer>() { @Override public Integer apply(@NotNull Integer integer) throws Exception { return integer + 1; } }).subscribe(new Consumer<Integer>() { @Override public void accept(Integer integer) throws Exception { System.out.println(integer); }});Copy the code
 public final <R> Observable<R> map(Function<? super T, ? extends R> mapper) {
        ...
        return RxJavaPlugins.onAssembly(new ObservableMap<T, R>(this, mapper));
    }
Copy the code
public final class ObservableMap<T, U> extends AbstractObservableWithUpstream<T, U> { final Function<? super T, ? extends U> function; public ObservableMap(ObservableSource<T> source, Function<? super T, ? extends U> function) { super(source); this.function = function; } @Override public void subscribeActual(Observer<? super U> t) { source.subscribe(new MapObserver<T, U>(t, function)); } static final class MapObserver<T, U> extends BasicFuseableObserver<T, U> { final Function<? super T, ? extends U> mapper; MapObserver(Observer<? super U> actual, Function<? super T, ? extends U> mapper) { super(actual); this.mapper = mapper; } @Override public void onNext(T t) { if (done) { return; } if (sourceMode ! = NONE) { downstream.onNext(null); return; } U v; Try {/ / after The data conversion to The downstream v = ObjectHelper requireNonNull (mapper. Apply (t), "The mapper function returned. A null value."); } catch (Throwable ex) { fail(ex); return; } downstream.onNext(v); } @Override public int requestFusion(int mode) { return transitiveBoundaryFusion(mode); } @Nullable @Override public U poll() throws Exception { T t = qd.poll(); return t ! = null ? ObjectHelper.<U>requireNonNull(mapper.apply(t), "The mapper function returned a null value.") : null; }}}Copy the code