具有通用类型的流图

Stream map with Generic type

我想要在 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());

声纳说:

Lambda 应替换为方法引用。 Method/constructor 引用比使用 lambda 更紧凑和可读,因此是首选。同样,空值检查可以替换为对 Objects::isNull 和 Objects::nonNull 方法的引用。

我想将 map(arg -> arg.getValue()) 改成 map(T::getValue()) 但编译错误 ().

Lambdas should be replaced with method references

改变

.map(arg -> arg.getValue())

.map(Argument::getValue)

至于:

Similarly, null checks can be replaced with references to the Objects::isNull and Objects::nonNull methods

我以前没有使用过 Sonar,但如果它更喜欢使用 Objects.isNullObjects.nonNull 进行空值检查,那么您需要这样做:

return Objects.isNull(args) ? null : args.stream()
                .map(Argument::getValue)
                .collect(Collectors.toList());