r/java • • Jul 16 '26

Identifying JDK value class candidates

https://mail.openjdk.org/archives/list/core-libs-dev@openjdk.org/thread/Y72NRXM7KYBX43OKYBQMVKOZDWKG4MHS/
52 Upvotes

59 comments sorted by

View all comments

Show parent comment

2

u/davidalayachew Jul 16 '26

So what I meant in my prev comment is that I think we can have Map.Entry declared "value record" class with a method:

public Entry setValue(T value) { return new Entry(key, value);}

But now you have changed the API, which makes it backwards-incompatible. That's a faux pas in the Java community without justifiable reason.

Remember, the method signature of Map.Entry#setValue is as follows.

public V setValue(final V newValue)

Which is to say -- it sets the new value, and returns the old. So, not only are you changing the API, but you are also removing pre-existing functionality. With your change, we can no longer return the old value, which would upset many users of this API.

Now, by all means, we could certainly add some sort of transform function that does what you say, but that still would not solve our current problem -- that Map.Entry does mutation. At best, we could have implementing subclasses that are value classes, and throw UnsupportedOperationException, but that's the best we can do. And even that is somewhat unsatisfying.