RFR: 8324718: Add a static function to java.util.Objects to simplify object equality checks [v6]
David Alayachew
duke at openjdk.org
Sat Jan 27 17:30:35 UTC 2024
On Sat, 27 Jan 2024 07:52:56 GMT, David Alayachew <duke at openjdk.org> wrote:
>> Adding a function to Objects in order to facilitate equality checking and enhance readability. You simply specify the 2 objects that you want to check for equality, and then provide the functions which will be used to provide the values that we will check for equality.
>
> David Alayachew has updated the pull request incrementally with one additional commit since the last revision:
>
> Rather than reiterating the precondition, let's explain why the method failed
> If you want a simpler, straightforward approach in case your getters change as often as the objects you compare, you can do:
>
> ```java
> getters.stream().allMatch(g -> Objects.equals(g.apply(a), g.apply(b)));
> ```
Oh, I did not see this when I clicked post.
So, this solution is great if I want all the fields. But again, ad-hoc. I don't always want all the fields. I want the user to be able to choose, at use-site, which methods to use to perform equality checking on.
Furthermore, not every method will be present in the class. What if I want to compare 2 objects for the length of their `String` field? I don't want to compare if the 2 are equal, but if the lengths of them are equal.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/17603#issuecomment-1913267350
More information about the core-libs-dev
mailing list