StableValue and non-deterministic iteration order

Per-Ake Minborg per-ake.minborg at oracle.com
Mon Sep 8 07:34:44 UTC 2025


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 <core-libs-dev-retn at openjdk.org> on behalf of Archie Cobbs <archie.cobbs at gmail.com>
Sent: Friday, September 5, 2025 10:50 PM
To: Pavel Rappo <pavel.rappo at gmail.com>
Cc: Ryan Ernst <ryan at iernst.net>; Kevin Bourrillion <kevin.bourrillion at oracle.com>; Stephen Colebourne <scolebourne at joda.org>; core-libs-dev <core-libs-dev at openjdk.org>
Subject: Re: StableValue and non-deterministic iteration order

On Fri, Sep 5, 2025 at 3:32 PM Pavel Rappo <pavel.rappo at gmail.com<mailto:pavel.rappo at gmail.com>> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20250908/12e866d7/attachment-0001.htm>


More information about the core-libs-dev mailing list