需要摆脱聚合操作中的三元运算符

Need to get rid of the ternary operator inside aggreagation operation

SonarQube 告诉我,我需要将三元运算符的这种用法转换为 "if"/"else" 结构。 我该怎么做?

public static String transform(final List<String> list) {

        return list.stream().filter(p -> p.matches(".*\d.*"))
                .reduce((a,
                        b) -> (Integer.parseInt(a.replaceAll("\D+", "")) > Integer
                                .parseInt(b.replaceAll("\D+", "")) ? a : b))
                .orElse("there are no numbers");

    }

三元运算符只是一个缩短的 if/else 表达式,您的代码相当于:

public static String transform(final List<String> list) {

    return list.stream()
            .filter(p -> p.matches(".*\d.*"))
            .reduce((a,b) -> {
                if (Integer.parseInt(a.replaceAll("\D+", "")) > Integer.parseInt(b.replaceAll("\D+", ""))) {
                    return a;
                } else {
                    return b;
                }
            })
            .orElse("there are no numbers");
}