<div dir="ltr">Thanks Johannes,<div><br></div><div>I just want to improve and contribute to the language.<br>Today, java.util.Random is useful and already helps me a lot.<br>If there's no way to insert the function, that's okay.<br></div><div><br></div><div><p>Best regards,</p><p>Daniel Perin Tavares<br></p></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Em seg., 25 de ago. de 2025 às 17:51, Johannes Döbler <<a href="mailto:jd@civilian-framework.org" target="_blank">jd@civilian-framework.org</a>> escreveu:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>
<div>
<div>Hi Daniel,<br>
<br>
what about providing List.getRandom() as an extension method [1]
in your projects?<br>
<br>
Best regards<br>
Johannes<br>
<br>
[1]
<a href="https://github.com/manifold-systems/manifold/blob/master/manifold-deps-parent/manifold-ext/README.md" target="_blank">https://github.com/manifold-systems/manifold/blob/master/manifold-deps-parent/manifold-ext/README.md</a><br>
<br>
<br>
<br>
On 23/08/2025 21:36, Daniel Tavares wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<p>Dear OpenJDK community,</p>
<p>I’d like to propose the addition of a <code>getRandom()</code>
default method to the <code>java.util.List</code> interface.</p>
<p>As a Java developer with over 15 years of experience, I’ve
often found myself needing to retrieve a random element from a
list. While this can be achieved using <code>ThreadLocalRandom</code>
or by shuffling the list, these approaches require boilerplate
code and are not immediately intuitive—especially for
newcomers to the language.</p>
<p><b>Motivation</b></p>
<p>Retrieving a random element from a list is a common task in
many domains:</p>
<ul>
<li>Games and simulations</li>
<li>Educational tools</li>
<li>Random sampling in data processing</li>
<li>Lightweight testing scenarios</li>
</ul>
<p>
</p>
<p>Adding a default method like <code>getRandom()</code> would
improve readability and reduce friction for developers,
particularly those learning Java or working on rapid
prototyping.</p>
<p><b>Proposed Method</b></p>
<p>default T getRandom() {<br>
if (isEmpty()) return null;<br>
int index = ThreadLocalRandom.current().nextInt(size());<br>
return get(index);<br>
}</p>
<p>Alternatively, the method could throw <code>NoSuchElementException</code>
if the list is empty, depending on what the community
considers more idiomatic.</p>
<p><b>Benefits</b></p>
<ul>
<li><strong>Improved developer experience</strong>: Simplifies
a common use case.</li>
<li><strong>Better readability</strong>: Expresses intent
directly.</li>
<li><strong>Minimal impact</strong>: Can be added as a default
method without breaking existing implementations.</li>
<li><strong>Alignment with modern Java</strong>: Leverages
default methods introduced in Java 8.</li>
</ul>
<p><br>
</p>
<p>I understand that additions to core interfaces are considered
carefully, and I welcome feedback on whether this idea aligns
with the design philosophy of the Java Collections Framework.</p>
<p>Thank you for your time and consideration.</p>
<p>Best regards,</p>
<p>
Daniel Perin Tavares<br>
Curitiba, Brazil</p>
</div>
</blockquote>
<br>
</div>
</blockquote></div>