SequencedSet.orderedSetOf + Map.orderedMapOf utility

Attila Kelemen attila.kelemen85 at gmail.com
Thu Dec 11 23:00:22 UTC 2025


Hi,

I don't know if I'm the only one, but I was always bothered by the lack of
immutable ordered sets in the JDK ever since `Set.of` and `Set.copyOf` were
added. This is an issue, because as is now people like to use the `Set.of`
methods (even moving from Guava's ImmutableSet), but since it doesn't
retain the order it is fairly annoying, because their unpredictable order
propagates to logs, json fields, etc. In fact, this annoying thing is even
promoted by silly Sonar rules like
https://rules.sonarsource.com/java/tag/java8/RSPEC-4738. Of course, a
similar thing can be said for `Map`.

Another, but not unrelated (but maybe surprising to some) reason for pain
is records. In a record you can't declare a component to be of type `A<T>`,
but take type `A<? extends T>` and create a defensive copy of the argument.
This is less bad in practice than it appears, because usually the case for
this is `List` and `Set` (`Map` is a bit bad though still, but is out of
scope for this email). But in each case, you can have a 2nd constructor
taking a `Collection`. However, in this case the 2nd constructor, you
likely have to make a copy of the `Collection`, and the canonical
constructor will do so again (which is an obvious waste). This is ok for
`List`, because `List.copyOf` won't actually do the copying the 2nd time.
However, for a `Set` I likely want to retain the order which - unless I use
a 3rd party library just for this (like Guava) - can be done by
`Collections.unmodifiableMap(new LinkedHashSet<>(src))`, which is wasteful
of course.

Are there any plans to plug these holes for `Set` and `Map` for the
collection framework?

Thanks,
Attila
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jdk-dev/attachments/20251212/9d3ad0b3/attachment-0001.htm>


More information about the jdk-dev mailing list