<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
</head>
<body>
<div></div>
See: https://bugs.openjdk.org/browse/JDK-8193128<br>
<div></div>
<div style="font-family: inherit; font-size: inherit; color: inherit; background-color: transparent;">
<br>
</div>
<div style="font-family: inherit; font-size: inherit; color: inherit; background-color: transparent;">
Jason</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 Ryan Ernst <ryan@iernst.net><br>
<b>Sent:</b> Friday, February 2, 2024 1:42 PM<br>
<b>To:</b> core-libs-dev@openjdk.org <core-libs-dev@openjdk.org><br>
<b>Subject:</b> Object creation from iterating Map.of()/Set.of()/List.of()</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">The newer “of” methods in collections are really nice, they make creating these collections much easier and often result in better performance.
<br>
However, the empty collection cases with Map.of()/Set.of()/List.of() has one small downside. The implementations within ImmutableCollections use non-specialized implementations for zero sized collections. For example, ImmutableCollections.EMPTY_MAP is a MapN.
 If you iterate over that Map, even if it is empty as in the case for Map.of(), a new anonymous AbstractSet is created. In comparison, Collections.emptyMap().entrySet() returns emptySet().<br>
<br>
I don’t know what the reasoning was for rebuilding the empty based variants in ImmutableCollections. But regardless, it seems like the empty collections defined in ImmutableCollections should likewise never construct any objects.<br>
<br>
I’m happy to raise a PR to either mimic or reuse the empty collection implementations from Collections, but I wanted to check there isn’t some reasoning the of() methods work this way.</div>
</span></font></div>
</body>
</html>