<div dir="ltr">Hello!<div><br></div><div>Several times already when writing Java programs, I stumbled with a simple task: given two objects with natural order, find the maximal of them. The algorithm I need could be formulated like this:</div><div><br></div>    <T extends Comparable<T>> T max(T a, T b) {<br>        return a.compareTo(b) > 0 ? a : b; <br>    }<br><div><br></div><div>Writing manually compareTo >= 0 looks too verbose, not very readable and error-prone: one has to mention both objects twice, and it's possible to mix > with <. I can surely add a method mentioned above to a utility class in my project and use it everywhere. However, I feel that it deserves a place in the standard library.</div><div><br></div><div>The alternatives we have now:</div><div>BinaryOperator.maxBy(Comparator.<T>naturalOrder()).apply(a, b);</div><div>This speaks clearly about the intent (we'd like to get the maximum and we write 'maxBy') but very wordy.</div><div><br></div><div>Stream.of(a, b).max(Comparator.naturalOrder()).get();</div><div>Also clear and a little bit shorter, but has an unnecessary Optional in-between (we know that we have at least one element, so the result is always present) and we have to mention the comparator. Finally, it might be much less efficient than expected.</div><div><br></div><div>Probably we can add simple static methods `max` and `min` either to the `Comparator` interface, or to `java.util.Objects`? Such methods would complement methods from the `Math` class for numbers. In addition, having default methods `max` and `min` in the `Comparator` interface would also be nice:<br><br>String bigger = String.CASE_INSENSITIVE_ORDER.max("Hello", "world");<br><br>What do you think? Can we proceed with such an enhancement?</div><div><br></div><div>With best regards,</div><div>Tagir Valeev</div></div>