In java, and there is one catch here. The objects are already compiled and the hash() and equals() methods therefore cannot be over written. Throwing it into a set, then back into a list will not work because the criteria for uniqueness isn’t currently defined in equals() and this cannot be overridden.
Share
You should still be able to create subclasses and create
equalsandhashcodemethods that work, unless the classes/methods arefinal.If that is the case, you could use composition, basically create a wrapper for the things you are putting in the collection, and have the wrapper’s
equalsandhashcodeimplement the contract correctly, for the thing being wrapped.You are in a tough position, because what I am reading is that the original classes are not following the contract for
equalsandhashcodewhich is a real problem in Java. It’s a pretty serious bug.