The non-deterministic iteration order of Immutable Collections
Jens Lideström
jens at lidestrom.se
Sun Mar 26 12:58:37 UTC 2023
> The problem is that everyone will have to pay for the extra storage cost for maintaining insertion order. Even if people don't need it.
I understand that.
The main point of my message is that I think it is worth the cost, that a fixed iteration order should be the default, and that it should be considered to be a niche use case to prioritize the memory savings of a random iteration order. This use case should be catered to by third-party libraries or alternative API:s.
These are a summary of the arguments (which I failed to provide in my original message):
* Many, probably most, uses of maps in Java benefit from a fixed iteration order.
* It is easy to get bugs or confusing behaviour by mistake when random iteration order is the default.
* The memory cost of an insertion-based iteration order is not very high.
* The Guava developers, and, as Remi notes, the Python developers, have also drawn this conclusion. (Argument from authority.)
Best regard,
Jens Lideström
On 2023-03-26 12:48, Kasper Nielsen wrote:
> On Sun, 26 Mar 2023 at 10:38, Jens Lideström <jens at lidestrom.se> wrote:
>> I think Map#of and friends would be more useful and less error prone if they where to return collections that have a fixed iteration order, where the order is defined by the insertion order when the map is created.
> The problem is that everyone will have to pay for the extra storage cost for
> maintaining insertion order. Even if people don't need it. Hence, why I
> suggested adding factory/copyOf methods for SequencedMap/SequencedSet
> for those people that need to rely on order. (Rereading Chris's email I know it
> wouldn't help his use-case).
>
> /Kasper
More information about the core-libs-dev
mailing list