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

On Tue, May 13, 2025 at 7:12 PM Tagir Valeev <amae...@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.
>

Reply via email to