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.
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?
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.
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.
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).
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.
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)
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.
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? :-)