如何化简三元表达式

How to simplify ternary expression

是否可以通过一行来简化这个 min + 三元表达式?

BigDecimal min = x.getMinimum();
BigDecimal result = otherValue.compareTo(min) > 0 ? otherValue : min;

看来你想要 BigDecimal.max

BigDecimal result = otherValue.max(x.getMinimum());

这将为您提供 otherValuex.getMinimum() 中的较大者。

(如果两者都不大于另一个,它将 return otherValue 而不是 x.getMinimum(),但这可能足够接近你想要的 - 替代方案是 x.getMinimum().max(otherValue).)