【发布时间】: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 的完整信息?
-
如果将
reduce替换为map(...).collect(...)会怎样? -
巧合:)。我只是这样做了,它奏效了。感谢您的帮助@Naman
-
fix y our inden tat ion
标签: java java-8 java-stream sonarlint functional-interface