简体   繁体   中英

How to simplify ternary expression

Is it possible to simplify this min + ternary expression by a one liner?

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

Seems like you want BigDecimal.max .

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

This will give you whichever is the greater of otherValue and x.getMinimum() .

(If neither is greater than the other, it will return otherValue rather than x.getMinimum() , but that is probably close enough to what you want — the alternative would be x.getMinimum().max(otherValue) .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM