Finding max or min of exactly two objects

Zheka Kozlov orionllmain at gmail.com
Sat May 24 10:35:59 UTC 2025


Collections.max(List.of(a, b))?

On Tue, May 13, 2025 at 7:12 PM Tagir Valeev <amaembo at gmail.com> wrote:

> The alternatives we have now:
> BinaryOperator.maxBy(Comparator.<T>naturalOrder()).apply(a, b);
> This speaks clearly about the intent (we'd like to get the maximum and we
> write 'maxBy') but very wordy.
>
> Stream.of(a, b).max(Comparator.naturalOrder()).get();
> 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250524/ce87b329/attachment.htm>


More information about the core-libs-dev mailing list