RXJAVA-Map

Posted 征服.刘华强

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RXJAVA-Map相关的知识,希望对你有一定的参考价值。

map是RxJava中最简单的一个变换操作符了, 它的作用就是对上游发送的每一个事件应用一个函数,

使得每一个事件都按照指定的函数去变化. 用事件图表示如下:

 

 

package com.netty.demo.vertx;

import io.reactivex.*;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RxJavaTest 

    public static void main(String[] args) throws InterruptedException 

        Observable.create(new ObservableOnSubscribe<Integer>() 
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception 
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
            
        ).map(new Function<Integer, String>() 
            @Override
            public String apply(Integer integer) throws Exception 
                return integer+"hello";
            
        ).subscribe(new Consumer<String>() 
            @Override
            public void accept(String o) throws Exception 
                log.info(o);
            
        );

        Thread.sleep(300000000);
    

 

以上是关于RXJAVA-Map的主要内容,如果未能解决你的问题,请参考以下文章