<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<font size="4" face="monospace">I'll just add: corelibs-dev is the
right place for this discussion, not amber-dev.</font><br>
<br>
<div class="moz-cite-prefix">On 10/16/2025 11:05 PM, Ethan McCue
wrote:<br>
</div>
<blockquote type="cite" cite="mid:CA+NR86gJdyt5oNLJkXeyiHJnTwKmTUwmN1LEJnCMfPhxZmC8pA@mail.gmail.com">
<div dir="ltr">I don't have anything intelligent to add, but I
assume either<br>
<br>
* It's a good / okay idea, not high priority<br>
* putIfAbsent and computeIfAbsent are seen to be enough for lazy
operations<br>
* Something much more subtle and/or annoying about how it will
affect the universe of map implementations.<br>
<br>
Either way, just so there is something concrete to talk about:<br>
<br>
<font face="monospace">import module java.base;<br>
<br>
public final class Maps {<br>
private Maps() {<br>
}<br>
<br>
private static final Object DEFAULT = new Object();<br>
<br>
@SuppressWarnings({"unchecked","rawtypes"})<br>
public static <K, V> V getOrCompute(<br>
Map<K, ? extends V> m, <br>
Object key, <br>
Function<? super K, ? extends V> compute<br>
) {<br>
Object value = ((Map) m).getOrDefault(key, DEFAULT);<br>
if (value == DEFAULT) {<br>
return compute.apply((K) key);<br>
}<br>
else {<br>
return (V) value;<br>
}<br>
}<br>
}</font>
<div><font face="monospace"><br>
</font></div>
<div><font face="arial, sans-serif">(Note the three needed casts
- this might be difficult for the same reason </font><font face="monospace">Map#get</font><font face="arial, sans-serif"> takes an </font><font face="monospace">Object</font><font face="arial, sans-serif">
and not a </font><font face="monospace">K</font><font face="arial, sans-serif">)</font></div>
</div>
<br>
<div class="gmail_quote gmail_quote_container">
<div dir="ltr" class="gmail_attr">On Thu, Oct 16, 2025 at
7:20 PM Chris Bouchard <<a href="mailto:chris@upliftinglemma.net" moz-do-not-send="true" class="moz-txt-link-freetext">chris@upliftinglemma.net</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div dir="auto">
<div>Alberto,</div>
<div dir="auto"><br>
</div>
<div dir="auto">I believe your getOrPut methods already
exist as putIfAbsent and computeIfAbsent, unless I'm
missing a subtle difference.<br>
<br>
<div class="gmail_quote" dir="auto">
<div dir="ltr" class="gmail_attr">On Thu, Oct 16, 2025,
14:20 Alberto Otero Rodríguez <<a href="mailto:alber84ou@gmail.com" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">alber84ou@gmail.com</a>>
wrote:</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div dir="ltr">
<div>Also, other two new methods might be
interesting if you want to get a value from a map,
but if the key doesn't exist you want to insert
that value in the map and return it:</div>
<div><br>
</div>
<div>
<div style="margin-left:40px">default
V getOrPut(Object key, V defaultValue)</div>
<br>
</div>
<div style="margin-left:40px">
default V getOrPut(Object key, Function<?
super K, ? extends V> defaultValueFunction)</div>
</div>
</blockquote>
</div>
</div>
<div dir="auto"><br>
</div>
<div dir="auto">Chris</div>
<div dir="auto">
<div class="gmail_quote" dir="auto">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
</blockquote>
</div>
</div>
</div>
</blockquote>
</div>
</blockquote>
<br>
</body>
</html>