在java 8中创建新代码后,我希望清理Sonar问题。
public class Argument<T> {
...
public T getValue() {
return parameterType.transform(group.getValues());
}
...
}我的代码:
List<Argument<?>> args = expression.match(text);
return args == null ? null : args.stream().map(arg -> arg.getValue()).collect(Collectors.toList());声纳说:
应该用方法引用替换Lambdas。方法/构造函数引用比使用lambda更紧凑和可读性更高,因此是首选。类似地,空检查可以替换为对对象::isNull和Objects:nonNull方法的引用。
我想通过map(arg -> arg.getValue())更改map(T::getValue()),但是编译()错误吗?
发布于 2019-09-27 22:11:09
应该用方法引用替换
Lambdas。
变化
.map(arg -> arg.getValue())至
.map(Argument::getValue)至于:
类似地,空检查可以替换为对对象::isNull和Object::
方法的引用。
我以前没有使用过Sonar,但是如果它更喜欢使用Objects.isNull和Objects.nonNull进行空检查,那么您就需要这样做:
return Objects.isNull(args) ? null : args.stream()
.map(Argument::getValue)
.collect(Collectors.toList());https://stackoverflow.com/questions/58141765
复制相似问题