Best implementation for hashCode method for a coll

2018-12-31 05:16发布

How do we decide on the best implementation of hashCode() method for a collection (assuming that equals method has been overridden correctly) ?

20条回答
不再属于我。
2楼-- · 2018-12-31 06:01

Use the reflection methods on Apache Commons EqualsBuilder and HashCodeBuilder.

查看更多
梦该遗忘
3楼-- · 2018-12-31 06:01

When combining hash values, I usually use the combining method that's used in the boost c++ library, namely:

seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);

This does a fairly good job of ensuring an even distribution. For some discussion of how this formula works, see the StackOverflow post: Magic number in boost::hash_combine

There's a good discussion of different hash functions at: http://burtleburtle.net/bob/hash/doobs.html

查看更多
登录 后发表回答