<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Rafael Winterhalter" <rafael.wth@gmail.com><br><b>To: </b>"core-libs-dev" <core-libs-dev@openjdk.java.net><br><b>Sent: </b>Thursday, January 16, 2025 8:13:17 AM<br><b>Subject: </b>Factory methods for SequencedSet and SequencedMap<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><div>Hello,</div></div></blockquote><div><br></div><div>Hello,<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><br><div>I am happily taking SequencedSet and SequencedMap into use, but one inconvenience I encounter is the lack of factory methods for the two.</div><br><div>In code where many (initial) collections have zero or one element (for later aggregation), I now write Set.of()/Set.of(one) and Map.of()/Map.of(key, value), as it makes the code shorter and more readable. Those collections are of course implicitly sequenced, but now I must make the variable type of the surrounding monad Set and Map, and simply assume that a LinkedHashSet or LinkedHashMap is used when a collection of more than one element is set, without requiring the interface type. This does not require any type casting, as I rely on the iteration order only, but the code loses some of its expressiveness.<br></div><br><div>I did not find any discussion around introducing factories for SequencedSet.of(...) and SequencedMap.of(...), similar to those that exist in the Set and Map interfaces. Was this ever considered, and if not, could it be?</div></div></blockquote><div><br></div><div>Thanks for re-starting that discussion, it was talked a bit, but not as it should be.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>So the issue is that currently we do not have any compact, unmodifiable and ordered Set/Map implementation,<br data-mce-bogus="1"></div><div>one use case is when you have data that comes from a JSON object as a Map and you want to keep the inserted order, if by example the JSON is a config file editable by a human, an other example is in unit tests where you want to help the dev to read the output of the test so the code that creates a Set/Map and what is outputed by the test should be in the same order.<br data-mce-bogus="1"></div><div>Currently there is no good solution for those use cases because Set|Map.copyOf() does not keep the ordering.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>I see two solutions, either we add a new SequenceSet|SequenceMap.of/copyOf, or we change the impleemntation of Set|Map.of()/copyOf().<br data-mce-bogus="1"></div><div>Python had gone for the latter solution, which has the advantage a being simple from the user POV, but from an algorithm expert POV, a Set and a SequencedSet are different concepts we may want to emphasis ?<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><div><br></div><br><div>Best regards, Rafael</div></div></blockquote><div><br></div><div>regards,<br data-mce-bogus="1"></div><div>RĂ©mi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div></div></div></body></html>