<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Hi Daniel, can you explain why `list.stream().findAny()` does not fulfill your needs? This also returns a random element and is also very concise.</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: "Calibri Light", "Helvetica Light", sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
-Chen Liang</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> core-libs-dev <core-libs-dev-retn@openjdk.org> on behalf of Daniel Tavares <cetr1n@gmail.com><br>
<b>Sent:</b> Saturday, August 23, 2025 2:36 PM<br>
<b>To:</b> core-libs-dev@openjdk.org <core-libs-dev@openjdk.org><br>
<b>Subject:</b> Proposal: Add getRandom() default method to java.util.List</font>
<div> </div>
</div>
<div>
<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>
</div>
</body>
</html>