Effective Java 英文 第二版 读书笔记 Item 9:Always override hashCode when you override equals

you must override hashCode in every class that overrides equals,particularly HashMap,HashSet,Hashtable.

The key provision that is violated when you fail to override hashCode is the second one:equal objects must have equal hash codes.

 

hashCode 生成基本方法,但是其实一般实体都有主键的存在,对主键hashCode即可,也能满足上面的约束;

 

小技巧 hashCode的结果可以缓存起来,不需要每次都计算

String,Integer,Date这些类的hashCode 返回这个实例的值,这不是一个很好方法,有待改进加强.

 

posted @ 2016-06-07 09:39  郁闷紫番薯  阅读(182)  评论(0编辑  收藏  举报