重构代码,以便使用此流程管道。

huangapple go评论64阅读模式
英文:

Refactor the code so this stream pipeline is used

问题

在SonarQube中遇到一个错误,在以下代码片段中重构代码,使得该流水线在接下来的代码片段中被使用。

Stream<ContextData> stream = Globals.contextMap.values().stream()
    .sorted(Comparator.comparing(ContextData::getName));

我们如何解决这个问题?

SonarQube提供的评论是,中间的Stream方法不应该被闲置未用。

但是在这行代码之后,我还对这个流进行了其他用途的操作,

String contextName = parameterParser.getTagsWithValue().get(FilterParameter.CONTEXT_NAME);
if (contextName != null) {
    stream = stream.filter(entity -> entity.getName() != null && entity.getName().equalsIgnoreCase(contextName));
}
英文:

Getting a bug in SonarQube with Refactor the code so this stream pipeline is used on following code snipts.

Stream&lt;ContextData&gt; stream = Globals.contextMap.values().stream()
.sorted(Comparator.comparing(ContextData::getName));

How we can solve this ?.

Comments provided by sonarQube is,
Intermediate Stream methods should not be left unused.

But i have some other use of this stream after this line,

String contextName = parameterParser.getTagsWithValue().get(FilterParameter.CONTEXT_NAME);
if (contextName != null) {
	stream = stream.filter(entity -&gt; entity.getName() !=null &amp;&amp; entity.getName().equalsIgnoreCase(contextName));
}

答案1

得分: 4

你应该关闭流:

例如,我将结果收集到一个列表中:

List<ContextData> contextDataList = Globals.contextMap.values().stream()
    .sorted(Comparator.comparing(ContextData::getName))
    .collect(Collectors.toList());
英文:

You should close stream :

ex. I collect the results to a list

List&lt;ContextData&gt;contextDataList = Globals.contextMap.values().stream()
.sorted(Comparator.comparing(ContextData::getName))
.collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年10月15日 19:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64370755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定