码迷,mamicode.com
首页 > 其他好文 > 详细

Reactor系列(十)collectMap集合

时间:2019-12-21 13:23:17      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:rabl   actor   mon   bsp   ext   rom   print   protected   string   

#java#reactor#collect#hashMap#

转换成Map

视频讲解: https://www.bilibili.com/video/av80048104/

技术图片

FluxMonoTestCase.java
package com.example.reactor;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Map;

@Slf4j
public class FluxMonoTestCase extends BaseTestCase {
    @Test
    public void collectMap(){
        Flux<Employee> employeeFlux = Flux.fromIterable(list);
        //转换HashMap
        Mono<Map<String,Employee>> mono = employeeFlux
                .collectMap(key ->key.getName(),val ->val);
        mono.subscribe(System.out::println);
    }
}
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));
}

结果:

{Alex=Employee(id=1, name=Alex, salary=1000.0), Owen=Employee(id=4, name=Owen, salary=1500.0), Denny=Employee(id=5, name=Denny, salary=2000.0), Michael=Employee(id=2, name=Michael, salary=2000.0), Jack=Employee(id=3, name=Jack, salary=1500.0)}

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

技术图片

Reactor系列(十)collectMap集合

标签:rabl   actor   mon   bsp   ext   rom   print   protected   string   

原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/12076505.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!