本文介绍了遍历Mono;Map<字符串、字符串&>并获得所需的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GenerateComplianceResponse()有一个订阅(),我对其进行了注释以突出显示它。问题是我不能在所有情况下都填充LocationsMap(),即使我保留了一个FlatMap()调用。SUBSCRIBE()有时有效,但并不是在所有情况下都有效,根据Spring文档,subsribe()并不总是保证结果。我希望在所有情况下都填充此LocationsMap(),以便我的结果一致。

    private Mono<Map<String,String>> locationsMap(){
        Map<String, String> map = new HashMap<>();
        map.put("DK", "Denmark");
        map.put("UK", "United Kingdom");
        System.out.println(map);
        return Mono.just(map);
    }

    private void generateComplianceResponse(BOLCompliance complianceResponse) {
        // locationsMap().subscribe();
        locationsMap().flatMap(entry -> {
            Set<java.util.Map.Entry<String, String>> set = entry.entrySet();
            set.forEach(e -> {
                if (complianceResponse.getCompliancePort().substring(0, 2).equalsIgnoreCase(e.getKey())) {
                    complianceResponse.setCountry(e.getValue());
                }
            });
            return Mono.just(complianceResponse);
        }).doOnNext(System.out::println).subscribe();
    }


public class BOLCompliance {
    private String complianceType;
    private String compliancePort;
    private String country;
}

推荐答案

我可以得到与Block()一致的结果。但我记得在过去,这个block()用来给出错误:block()/lockfirst()/locklast()都是阻塞的,线程不支持这一点。但对于这段代码,它不会给出任何

private void generateComplianceResponse(BOLCompliance complianceResponse, BOLReference bill) {
        complianceResponse.setStatus(bill.getStatus()!=null?bill.getStatus():null);
        complianceResponse.setErrorDetails(bill.getErrorDetails());
        getCustomsStatus(complianceResponse, bill);
        getComplianceCountry(complianceResponse).block();
    }

    private Mono<BOLCompliance> getComplianceCountry(BOLCompliance complianceResponse) {

        return locationMap().flatMap(entry -> {

        Set<java.util.Map.Entry<String, String>> set = entry.entrySet();
         set.forEach(e -> {
                if (complianceResponse.getCompliancePort().substring(0, 2).equalsIgnoreCase(e.getKey())) {
                    complianceResponse.setCountry(e.getValue());
                }
            });
            return Mono.just(complianceResponse);
        });
    }

这篇关于遍历Mono;Map&lt;字符串、字符串&>并获得所需的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:51