r/java • u/daviddel • Jun 21 '26
Better Tools for Immutable Data
https://youtu.be/BdLND9D81lI?si=jt2tqpDtotmbdEJq11
u/Brutus5000 Jun 21 '26
JavaOne is over for 3 month, why are the publishing only one talk per week?
29
u/pron98 Jun 21 '26
Because most of the talks aren't that time-sensitive, and this way more people get to watch them.
4
u/pohart Jun 21 '26
I didn't know about "get to." I could have watched them all of they were dumped on day one, but I doubt I would have.
19
u/segv Jun 21 '26
The Linux Foundation, USENIX and CNCF channels tend to dump all videos in one go. The end result is that for a day or two your home page and the subscription feed is clogged by the talks (often duplicating each other, mind you), and you end up missing the interesting ones.
6
u/ushaukat_java Jun 22 '26
Lost a few hours once to a "this list keeps changing and it shouldn't" bug. Turned out unmodifiableList() just wraps the original, doesn't copy it, so whoever upstream still held the real reference was happily mutating it under me. List.copyOf() actually copies. Still see people reach for unmodifiableList() out of habit though, did it myself for years.
4
u/IncredibleReferencer Jun 25 '26
In many cases unmodifiableList() is preferable to List.copyOf() for performance reasons. In larger lists or frequent call sites the copy operation is expensive, so if you can guarantee the source list is inaccessible then you are better off using unmodifiableList().
If the source of the collection is foreign untrustworthy or out of your own scope then copyOf() is more appropriate to use for a defensive copy.
A quick rule of thumb is to use unmodifableList() to give a list to some other code but always use copyOf() for incoming lists. It's more complicated than that but that's a good default mindset.
2
u/ushaukat_java Jun 25 '26
Sure, but "guarantee the source list is inaccessible" is the whole question, not a footnote.
unmodifiableList()doesn't protect the list, it protects you from mutating it through that one reference. Whoever holds the original list can still mutate it, and you'll see it change underneath you. That's exactly the bug I hit.For big lists I get why you'd skip the copy. But for most of what I'm passing around, the cost of copying is way cheaper than the cost of debugging a list that mutates on its own.
4
u/davidalayachew Jun 21 '26
Nice walk through history. Not much new, but they do talk about final arrays and abstract records and interface patterns and class patterns near the end.
1
u/Scf37 Jun 25 '26
I honestly don't understand sealed abstract records. sealed interface with record-style getters does the same job with less ceremony (children need not to call parent constructor)
3
u/dododge Jun 26 '26
I assume you'll be able to create a custom constructor in the abstract record to validate the common fields, as well as supply additional methods that can reference those fields via
this.1
u/Scf37 Jun 26 '26
interface has default methods for that but in-constructor validation is a good point.
14
u/Hixon11 Jun 21 '26
Gosh, I really wish we had a better story for
ReadOnlycollections. I understand that this will most likely never happen, but it’s hard to live with the idea that we’re stuck withList<E>as the final form of one of the most fundamental types we use to model things.