Reactor系列(五)map映射

2020-08-08T23:09:14
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

#java# #reactor# #flux# #map# #映射#

视频解视: https://www.bilibili.com/video/av79179444/

FluxMonoTestCase.java

package com.example.reactor;
 
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
 
@Slf4j
public class FluxMonoTestCase extends BaseTestCase {
    @Test
    public void map() {
        Flux<Employee> employeeFlux = Flux.fromIterable(list);
        employeeFlux.filter(employee -> employee.getSalary() == 2000)
                .map(employee -> {
                    Leader leader = new Leader();
                    leader.setName(employee.getName());
                    leader.setSalary(employee.getSalary());
                    return leader;
                }).log().subscribe();
        employeeFlux.map((in) -> {
            Leader leader = new Leader();
            leader.setName(in.getName());
            leader.setSalary(in.getSalary());
            return leader;
        });
    }
 
    @Data
    @ToString
    class Leader {
        private String name;
        private double salary;
    }
}

BaseTestCase.java

package com.example.reactor;
 
import java.util.Arrays;
import java.util.List;
 
public class BaseTestCase {
    protected static final List<Employee> list = Arrays.asList(
            new Employee(1, "Alex", 1000),
            new Employee(2, "Michael", 2000),
            new Employee(3, "Jack", 1500),
            new Employee(4, "Owen", 1500),
            new Employee(5, "Denny", 2000));
}

结果: 

11:37:33.668 [main] INFO reactor.Flux.MapFuseable.1 - | onSubscribe([Fuseable] FluxMapFuseable.MapFuseableSubscriber)
11:37:33.670 [main] INFO reactor.Flux.MapFuseable.1 - | request(unbounded)
11:37:33.670 [main] INFO reactor.Flux.MapFuseable.1 - | onNext(FluxMonoTestCase.Leader(name=Michael, salary=2000.0))
11:37:33.681 [main] INFO reactor.Flux.MapFuseable.1 - | onNext(FluxMonoTestCase.Leader(name=Denny, salary=2000.0))
11:37:33.682 [main] INFO reactor.Flux.MapFuseable.1 - | onComplete()

关注公众号,坚持每天3分钟视频学习

扫一扫关注公众号添加购物返利助手,领红包
当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »
因本文不是用Markdown格式的编辑器书写的,转换的页面可能不符合MIP标准。