Sonarlint提示“重构代码,以便使用流管道”

问题描述 投票:0回答:1

我正在使用基于流的方法将我的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;
            };

java java-8 java-stream sonarlint functional-interface
1个回答
0
投票

更新::我已经找到了解决此问题的方法,方法是将map-reduce操作拆分为map和collect操作,如下所示。现在出现该特定的皮棉错误

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;
            };
© www.soinside.com 2019 - 2024. All rights reserved.