I have several sets that store objects of the same class, but I want to specify a different identification function for each of them (for example, in one set A == B, if Ax == Bx, and in the other A == B if Ay == By).
I am currently using TreeSets with different Comparators defined for each. I am wondering how you can do the same if I want to switch to HashSets. Java does not allow you to pass a separate hash function in the same way that it allows you to compare comparators for sorted / tree-based collections. The only way I can do this is to create another wrapper class and implement the hashCode () method for each of the elements of each HashSet. Is there a better way to do this?
source
share