Proposal to extend Math.min and Math.max to support multiple parameters

Chen Liang chen.l.liang at oracle.com
Fri Jan 24 21:42:46 UTC 2025


On a side note, there are already Collections.min and Collections.max. We can use Collections.min(Arrays.asList(v1, v2, v3)) to find the minimum, too, and it works for antique Java versions.

Regards, Chen
________________________________
From: core-libs-dev <core-libs-dev-retn at openjdk.org> on behalf of Joseph D. Darcy <joe.darcy at oracle.com>
Sent: Friday, January 24, 2025 3:12 PM
To: Aviad Zer <aviad1486 at gmail.com>; core-libs-dev at openjdk.org <core-libs-dev at openjdk.org>
Subject: Re: Proposal to extend Math.min and Math.max to support multiple parameters


Hello Aviad,

A brief reply here, there is some utility in having direct support for finding the min or max over an aggregate of data and min/max methods taking an array is something that could have been added to the platform at any point since its inception. The fact that such methods weren't added before doesn't completely rule out adding them now, but does give pause. Additionally, with streams finding the min/max is an easy operation so I would see less justification to adding array-based min/max in JDK 25+ than before streams were added in JDK 8.

An analysis showing existing use of array-based min/max method as JDK-internal APIs would strengthen the case, but I believe the default solution at this point is "use streams."

HTH,

-Joe

On 1/22/2025 9:45 PM, Aviad Zer wrote:

Dear Core-Libs Dev Team,

I hope this message finds you well.

I would like to propose an enhancement to the Math class to extend the Math.min and Math.max methods to support multiple parameters using varargs. This enhancement aims to improve code readability and provide a more intuitive way to find the minimum or maximum value among multiple numbers without requiring nested calls or external logic.

Motivation:
Currently, developers must either nest multiple calls to Math.min or use the Streams API (Arrays.stream(values).min()), which can feel cumbersome for simple cases. By introducing varargs-based Math.min and Math.max methods for int, long, float, and double, this proposal simplifies the process and aligns with the expected functionality of these utility methods.

Current Progress:
I have submitted a pull request for this enhancement: PR #23138<https://github.com/openjdk/jdk/pull/23138>. The PR includes:

  *   Implementation of Math.min and Math.max with varargs for int, long, float, and double.

Feedback and Next Steps:
I am reaching out to gather your feedback on this proposal and determine whether this enhancement aligns with the goals of the core-libs. I understand that significant changes like this may require a CSR, and I am fully prepared to follow the process once I have a response from the community.

Thank you for your time and consideration. I look forward to hearing your thoughts and engaging with the community on this proposal.

Best regards,

Aviad Zer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250124/b96fc56e/attachment-0001.htm>


More information about the core-libs-dev mailing list