If an object holds a unique primary key, what interfaces does it need to implement in order to be collection friendly especially in terms of being efficiently sortable, hashable, etc…?
If the primary key is a string, how are these interfaces best implemented?
Thanks!
You must override
Object.equals()andObject.hashCode(), and also implement theComparableinterface. This will make your class fully “compliant” when doing any kind of sorting or hashing including usingCollections.sort(), anyMapclass, or anySetclass. If there’s even a tiny chance that the class will be put in some sort of collection, then it should definitely implement all three of these methods.Keep in mind that if two objects are equal, then:
compareTo()must return 0.