When using DelayQueue Java, should equals () and hashCode () be used?

I am currently working on a class using DelayQueue . I noticed that since objects in DelayQueue implement the Delayed interface, these objects also need to implement the compareTo() method, which has already been executed.

Does this mean that I should also consider using the equals() method and the hashCode() method?

The reason I ask is because I came across this advice when searching for a project through FindBugs , and I'm trying to figure out if this is necessary or not for this particular case.

+1
java static-analysis findbugs
source share
1 answer

As a good practice, yes, since equals , hashCode and compareTo have close values. They can be considered as different aspects of the same thing. If you mind being used elsewhere without implementing them together, you may encounter unpredictable behavior.

For example, you passed your object to a third-party library that uses a binary search algorithm, uses compareTo . A few months later, the new version of the library changed to a hashed data structure to improve performance, which are relayed to equals and hashCode . From their point of view, this does not violate the changes.

As in this case, no, because DelayQueue does not use them.

+8
source share

All Articles