JDK 21 Sequenced Collections

132 views
Skip to first unread message

Rhys Saldanha

unread,
Jan 5, 2024, 9:50:15 AMJan 5
to guava-discuss
Hi all,

After reading JEP 431, it occurred to me that many of the Guava collections are sequenced. For example, ImmutableSet and ImmutableMap preserve insertion order - a property that I often rely on.

Would it be possible to get a JDK 21 flavour of Guava, specifically so that Immutable Collections can implement SequencedCollection interfaces where applicable?

Thanks,
Rhys

Chris Povirk

unread,
Jan 5, 2024, 10:52:50 AMJan 5
to Rhys Saldanha, guava-discuss
Thanks, there have been some scattered discussions of this but nothing that pulls them all together.

One of the things I've wondered about SequencedCollection is how much users benefit from having a common interface declared vs. just adding getFirst(), getLast(), etc. to the types where it could be useful. I haven't tried to dig up design discussions about this, so if you know of any, or if you have some experience in practice with this, I'd be very interested.

To actually answer your question: My guess is that we wouldn't introduce another flavor of Guava, given the complexities to us and to users of having even two. (Beyond the Maven confusion, I wonder if a flavor with an additional supertype for ImmutableSet would be relatively more likely to lead to source incompatibilities for users who switch between the two flavors of Guava. It's probably not going to come up much?) Or at least we'd want to accumulate a bunch of reasons (perhaps including SequencedCollection) to convince ourselves that it will pay off. It's not like it's totally out of the question, especially if the only changes we needed to make were trivial and automatable, as they could be here.

(An alternative that users have raised before is multi-release jars. However, as you may already be aware, those are intended only for the case in which the APIs match exactly. And when the APIs don't match, we've seen problems.)

Even today, we probably could getFirst(), getLast(), etc. to ImmutableSet and friends without implementing the interface. In fact, we did that, at least for ImmutableSortedSet (because doing so addressed another problem we'd encountered). However, we found that this could cause problems for Android users in some cases, so we rolled it back. Additionally, we later discovered that, if we'd build Guava with JDK 21+, then the resulting jar wouldn't have worked with older JDKs. (Ideally, we'd build with --release 8, but that's a whole project of its own....)

There's also the fact that we might complicate building Guava with older JDKs. (Compare #6549.) But we could probably work that out with enough clever Maven setup.

Do you want to open an issue to track this? Even if I'm guessing that we ultimately won't do it, it would be good to accumulate the possible reasons for doing so and also the possible obstacles.

Rhys Saldanha

unread,
Jan 6, 2024, 6:45:48 AMJan 6
to guava-discuss
I'm happy to raise an issue!

Given that the SequencedCollection interface only exists in JDK 21+, I don't see how we could ever build a JDK >21 compatible JAR. This wouldn't solve the problem and is a separate conversation, but it might be time to increase the minimum Java version of 11 or even 17? (As a Spring Boot 3 user, I am used to requiring a minimum Java version of 17.)

In terms of user benefit, for me, it's mostly a case of documentation. The new methods that come with the interfaces are a bonus, but I want to make it a little easier to explain that the Guava ImmutableCollections are special and sometimes preserve insertion order where their Java counterpart wouldn't (Set!). I appreciate that the javadoc on ImmutableCollection already makes this clear - "Deterministic iteration", but being able to pass around Sequenced interfaces would communicate that the order of some collection is important.

Having said this, I am still waiting for my company to graciously allow us to use JDK 21 in production code, so I haven't yet practiced what I'm preaching.

Chris Povirk

unread,
Jan 8, 2024, 4:30:03 PMJan 8
to Rhys Saldanha, guava-discuss
(For anyone interested in Sequenced Collections, there's now more in https://github.com/google/guava/issues/6903.)
 
but it might be time to increase the minimum Java version of 11 or even 17?

I basically don't dare even dream about such things yet :) We have to be very conservative, given that some of our users are very conservative.

At the moment, we have an issue open only for requiring Java 11+ to build Guava: https://github.com/google/guava/issues/6549. Something along those lines might help us use SequencedCollection conditionally in the future (if only in limited cases like Iterables.getLast... for which we might just use MethodHandle instead).

(I had a note to myself to also say something here about Android, but I'm not sure what it would have been....)
Reply all
Reply to author
Forward
0 new messages