使用Map和lambda表达式优化java代码

d6kp6zgx  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(269)

我有这段代码,它获取“rate”的数据,如果这些参数中的任何一个没有足够的值,我必须将值从参数更改为字符串“all”,那么什么是de-best方法呢?我的实际代码是:

Map<String, List<Example>> exampleMap= cacheExampleData.getExample();
BigDecimal rate = exampleMap
    .get(RequestDto.getCustomer() == null ? "ALL" : RequestDto.getCustomerSubsegment())
    .stream().filter(x -> x.getSaleChannel().equalsIgnoreCase(RequestDto.getChannelId()))
    .filter(x -> x.getFlowName().equalsIgnoreCase(flowName))
    .filter(x -> x.getCustomerType().equalsIgnoreCase(RequestDto.getCustomerSegment()))
    .map(Example::getRate).findFirst().orElse(null);

if (rate == null && RequestDto.getCustomerSubsegment() != null) {
     rate = exampleMap
    .get(Constant.All)
    .stream().filter(x -> x.getSaleChannel().equalsIgnoreCase(RequestDto.getChannelId()))
    .filter(x -> x.getFlowName().equalsIgnoreCase(flowName))
    .filter(x -> x.getCustomerType().equalsIgnoreCase(RequestDto.getCustomerSegment()))
    .map(Example::getRate).findFirst().orElse(null);
 }

if (rate == null) {
     rate = exampleMap
    .get(Constant.All)
    .stream().filter(x -> x.getSaleChannel().equalsIgnoreCase(Constant.All)
    .filter(x -> x.getFlowName().equalsIgnoreCase(flowName))
    .filter(x -> x.getCustomerType().equalsIgnoreCase(RequestDto.getCustomerSegment()))
    .map(Example::getRate).findFirst().orElse(null);
 }

.
.
.
if (rate == null) {
     Do the same but with ALL in the first and second...X element
 }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题