SequencedSet.orderedSetOf + Map.orderedMapOf utility
Pavel Rappo
pavel.rappo at gmail.com
Fri Dec 12 00:05:43 UTC 2025
You're definitely not the only one; see
https://bugs.openjdk.org/browse/JDK-8156081
On Thu, Dec 11, 2025 at 11:00 PM Attila Kelemen
<attila.kelemen85 at gmail.com> wrote:
>
> 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
More information about the jdk-dev
mailing list