Proposal: Add getRandom() default method to java.util.List

Alan Bateman alan.bateman at oracle.com
Sun Aug 24 09:42:15 UTC 2025


On 23/08/2025 20:36, Daniel Tavares wrote:
> :
>
> *Proposed Method*
>
> default T getRandom() {
>     if (isEmpty()) return null;
>     int index = ThreadLocalRandom.current().nextInt(size());
>     return get(index);
> }
>
Add Lists that are mutable and support concurrent access to your list to 
think about. These implementations would need to be updated to implement 
this method, otherwise you risk getting IOOBE with the default method.

-Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250824/de6b01f2/attachment-0001.htm>


More information about the core-libs-dev mailing list