Stable Map Background
The iteration order of `StableValue.map()` is not specified, and the returned 
map is not an instance of SequencedMap. The current implementation happens to 
rely on Map.of() for which the iteration order of mappings is specified to be 
unspecified and subject to change (i.e., indeterministic). This was a 
deliberate design choice. Hence, StableValue.map() currently has the same 
iteration order properties as Map.of().

The Way Forward for Stable Map
There are several alternatives for the future, all of which have different pros 
and cons, including but not limited to, and in no particular order (pun not 
intended):
 * The iteration order remains unspecified as today (and could be 
fixed/random/always having 42 at the front/...),
 * We specify that the order is unspecified and subject to change (like 
Map.of()),
 * We specify that the order is unspecified but will always remain the same 
(i.e., not subject to change/is deterministic),
 * We specify that the order is specified and coincides with the iteration 
order of the given Set at construction time, or
 * We allow the client to provide a Comparator that decides the iteration order

I think there is a more general discussion to be had that should be aligned 
with the direction of the immutable collections and any potential constructs 
with deterministic order there.

Best, Per
________________________________
From: core-libs-dev <[email protected]> on behalf of Archie Cobbs 
<[email protected]>
Sent: Friday, September 5, 2025 10:50 PM
To: Pavel Rappo <[email protected]>
Cc: Ryan Ernst <[email protected]>; Kevin Bourrillion 
<[email protected]>; Stephen Colebourne <[email protected]>; 
core-libs-dev <[email protected]>
Subject: Re: StableValue and non-deterministic iteration order

On Fri, Sep 5, 2025 at 3:32 PM Pavel Rappo 
<[email protected]<mailto:[email protected]>> wrote:
Be it Map.of or HashMap, there's no user-defined order. The only
user-defined order one can currently get is by using LinkedHashMap. In
the future though, we may get SequencedMap.of.

I vote for adding SequencedMap.of(). That seems like a good solution to the 
problem at hand, as well as a generally useful addition.

Of course if you believe that argument, then SequencedSet.of() would also be a 
good addition, since Set.of() also makes no ordering guarantees.

-Archie

--
Archie L. Cobbs

Reply via email to