【问题标题】:Sonarlint prompting to "Refactor code so that stream pipeline is used"Sonarlint 提示“重构代码以便使用流管道”
【发布时间】:2020-05-12 12:05:36
【问题描述】:

我使用基于流的方法将我的 List<Map<String,String>> 映射减少到 List<CustomObject> 。以下代码用于流

List<Map<String,String>> mailVariable = (List<Map<String, String>>) processVariables.get("MAIL_MAP");

1| List<CustomObject> detList = mailVariable
2|                                      .stream()
3|                                      .flatMap(getEntry)
4|                                      .filter (isEmpty)
5|                                      .reduce(new ArrayList<CustomObject>(),accumulateToCustomObject,combiner);

我正在使用 sonarLint 分析我的代码,并在第 2 行和第 3 行得到以下错误

重构此代码以便使用流管道。鱿鱼:S3958

我实际上正在使用流并按照建议here 从终端操作中返回值。有什么我做错了吗?任何人都可以建议编写此代码的正确方法吗?

// following are the functional interface impls used in the process

Function<Map<String,String>, Stream<Entry<String,String>>> getEntry = data -> data.entrySet().stream();

Predicate<Entry<String, String>> isEmpty                            = data -> data.getValue() != null
                                                                                    || !data.getValue().isEmpty() 
                                                                                    || !data.getValue().equals(" ");

BinaryOperator<ArrayList<CustomObject>> combiner                = (a, b) -> {
                                                                                ArrayList<CustomObject> acc = b;
                                                                                acc.addAll(a);
                                                                                return acc;
                                                                            };

BiFunction<ArrayList<CustomObject>,Entry<String,String>,ArrayList<CustomObject>> accumulateToCustomObject = (finalList, eachset) -> {
                /* reduction process happens            
                   building the CustomObject..
                */
                return finalList;
            };

【问题讨论】:

  • 什么是 SQID?还是来自 Sonar 的完整信息?
  • 这发生在哪个版本的声纳上?你试过 4.15 吗?原因 = thisthisthat
  • 如果将reduce 替换为map(...).collect(...) 会怎样?
  • 巧合:)。我只是这样做了,它奏效了。感谢您的帮助@Naman
  • fix y our inden tat ion

标签: java java-8 java-stream sonarlint functional-interface


【解决方案1】:

更新:: 我找到了解决此问题的方法,方法是将我的 map-reduce 操作拆分为一个 map 并像这样收集操作。现在出现了那个特定的 lint 错误

List<AlertEventLogDetTO> detList = mailVariable
                                                    .stream()
                                                    .flatMap(getEntry)
                                                    .filter (isEmpty)
                                                    .map(mapToObj)
                                                    .filter(Objects::nonNull)
                                                    .collect(Collectors.toList());

Function<Entry<String,String>,AlertEventLogDetTO> mapToObj = eachSet -> {
                String tagString = null;
                String tagValue  = eachSet.getValue();

                try{
                    tagString = MapVariables.valueOf(eachSet.getKey()).getTag();
                } catch(Exception e){
                    tagString = eachSet.getKey();
                }

                if(eventTags.contains(tagString)){
                    AlertEventLogDetTO entity = new AlertEventLogDetTO();
                    entity.setAeldAelId(alertEventLog.getAelId());
                    entity.setAelTag(tagString);
                    entity.setAelValue(tagValue);

                    return entity;
                }

                return null;
            };

【讨论】:

    猜你喜欢
    • 2021-01-29
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    相关资源
    最近更新 更多