Need to get rid of the ternary operator inside aggreagation operation

89 Views Asked by At

SonarQube says me that i need to convert this usage of the ternary operator to an "if"/"else" structure. How can i do that?

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");

    }
1

There are 1 best solutions below

0
Aquarthur On BEST ANSWER

The ternary operator is just a shortened if/else expression, your code is the equivalent of:

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");
}