r/java • • Feb 04 '26

Ask the Java Architects with Brian Goetz and Viktor Klang (Jfokus 2026)

https://youtu.be/HB56zU3pTmQ
55 Upvotes

30 comments sorted by

View all comments

9

u/Enough-Ad-5528 Feb 05 '26

Hopefully someday my employer sponsors me to go to one of these. I have always wanted to ask this question:

I have gone through the official Oracle explanation doc (https://docs.oracle.com/javase/8/docs/technotes/guides/collections/designfaq.html#a1) that thoroughly explained why the current collection classes cannot be retrofitted with proper immutable interfaces - that was a good read and I have to concede that the current collection classes hierarchy cannot accommodate proper immutable interfaces.

I also think at one point, someone from the JDK team said that there isn't enough value in just having proper immutable Collection classes to come up with a complete new Collections api distinct from the existing one and I think I have to agree with that.

However, I do want to ask them now, with so many new language/library features already in the JDK since Java 5, or currently in the pipeline (Optional, LazyConstant, final-really-is-final, the new serialization proposal, sealed classes, records, pattern matching, value classes, nullable types/markers, Streams, Lambdas) is there enough new stuff in the language now that a new Collections API seem worth it?

And If not, is there some sufficient condition (language/vm features), where the JDK team would be open to thinking about a new API that addresses some of the rough edges in the current API? Or has that ship sailed and a new collections api disjoint from the existing one is basically non-starter now? Java did something similar for java.time package so I am hoping we can have that for collections as well even though I do agree this is much larger.

java.collections.* please? :-)

11

u/pron98 Feb 05 '26 edited Feb 05 '26

As always, the "sufficient condition" would be a sufficiently serious problem. The issue is that any addition has a cost, and it's not mainly the opportunity cost, but also the cost of making the platform more complicated (e.g. collections in C#, Kotlin, and Scala don't justify their complexity IMO).

Can you describe the problem(s) you've experienced without mentioning any possible solution?

5

u/ZimmiDeluxe Feb 06 '26

In my experience, most problems go away if you don't mutate collections you didn't create in the same method (scoped mutability, if you will). I usually wrap returned collections into List::copyOf, Set::copyOf or Map::copyOf, so callers don't come to rely on mutability and break when you add an early return List.of() in the future. If the returned collection is used a lot, the cost of the copy might even be offset by the read-optimized unmodifiable implementation.

2

u/aoeudhtns Feb 06 '26

If you're not opposed to adding a dependency, Eclipse Collections has a bunch of these distinctions baked in, if you actually need it. Now that may be of limited use to you - you may not want to add a dependency, or expose a 3rd party interface in your own API - but in terms of just getting something done, it can be helpful.

1

u/8igg7e5 Feb 05 '26

I do wonder if a simple unmodifiable and/or immutable marker-interface could help out (a la RandomAccess).

This would provide a convention (implemented consistently in JDK types) for applications to bypass defensive copying when you may have types that are not in the JDK, but can have these properties (though you might do defensive copying anyway in security contexts to prevent fibbing about it, and undermining trust).

The JDK has some scenarios where it can opt out of copying because of the special knowledge of internal types, but it misses in far too many cases (and it's a tool only the JDK can use).

Essentially supporting this across more types:

this.list = incomingList instanceof Unmodifiable
    ? incomingList
    : List.copyOf(incomingList);

2

u/aoeudhtns Feb 06 '26 edited Feb 06 '26

You actually don't need that. If you look at copyOf implementation, it already detects its internal unmodifiable wrapper and then just passes it back out if it's already that. So, feel free to defensively copyOf as much as you want!

Now, if you're talking about some sort of universal JDK feature, then that would be a different beast.

2

u/8igg7e5 Feb 06 '26

I was acknowledging that behaviour already - but it only works for unmodifiable types inside the JDK. Those aren't the only Collection implementations - and it doesn't support the Collections.unmodifiableList(...) wrappers.

At the moment we see a lot of unwanted and unnecessary list-copying. It would be good for the developer to be able to express that this list is safe to skip defensive copies without having to make a copy with List.of (paying a toll every time just in case the list might be passed to someone who wants the safety of a stable list)

1

u/aoeudhtns Feb 06 '26

I'm sorry. My eyes jumped right over that 3rd para/sentence. Too eager to get to the codeblock.

1

u/kaperni Feb 06 '26

Also want to add https://nipafx.dev/immutable-collections-in-java/ to mix.

I can't imagine the pain of trying to retrofit a new "java.collections.*" package into the ecosystem. This would be magnitudes more painful than when generics was added.