开发者

Defining muliple hash functions for the same object

开发者 https://www.devze.com 2022-12-15 10:58 出处:网络
I have several Sets that store the objects of the same class, but I want to specify a different identity function for each of them (i.e. say, in one set A==B if A.x==B.x, while in another A==B if A.y=

I have several Sets that store the objects of the same class, but I want to specify a different identity function for each of them (i.e. say, in one set A==B if A.x==B.x, while in another A==B if A.y==B.y).

Currently I use TreeSets with different Comparators defined for each. I am wondering how the same thing can be done if I want to switch to HashSets. Java does not allow passing a separate hash function in the same way it allows Comparators for Sorted/Tree-based Collections. The only way that I can think of doing 开发者_Python百科it would involve creating a different wrapper class and implementing the hashCode() method in each for the elements of each HashSet. Is there a better way of doing this?


How about creating separate subclasses for each set. The only difference for each subclass would be the overridden hash function that meets your criteria.


I'm not aware of a better way to do it. Your proposed solution (wrapper classes with different comparison logic) sounds very reasonable.

You mentioned implementing the hashCode method - don't forget to implement equals as well.


You could try using THashSet in GNU Trove, This support multiple hashing strategies.


There is another option: You can copy the source for HashMap and replace the method hash(Object key) with something else (for example, a call to a Hasher that works like the Comparator of the TreeMap).

0

精彩评论

暂无评论...
验证码 换一张
取 消