声纳林特提示"Refactor code so that stream pipeline is used"



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

事实上,我正在使用流并按照此处的建议从终端操作中重新设置值。我做错了什么吗?有人可以建议编写此代码的正确方法吗?

// 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;
};

更新::我通过将我的map-reduce操作拆分为地图并像这样收集操作,找到了解决此问题的方法。现在出现了那个特定的 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;
};

最新更新