sometimes the default implementation of equals has the desired behaviour (as in a type-safe enumeration, for example), but equals should usually compare state, not identity. this is particularly true for "data-centric" classes which map to database records.
if you override equals, you must override hashcode hashcode must generate equal values for equal objects objects placed in a list , set, or map (as either a key or value) should have an appropriate definition of equals. (see, for example, the javadoc for java.sun.com/j2se/1.5.0/docs/api/java/util/collection.html#contains(java.lang.object)">collection.contains , java.sun.com/j2se/1.5.0/docs/api/java/util/map.html#containskey(java.lang.object)">map.containskey, and java.sun.com/j2se/1.5.0/docs/api/java/util/map.html#containsvalue(java.lang.object)">map.containsvalue .)if you extend a concrete class, and add a new field which contributes to equals, then it is not possible to write a perfectly correct equals method for the new class. instead, you should use composition instead of inheritance. 【程序编程相关:ANT介绍及安装及配置 】
hashcode and equals are closely related : 【推荐阅读:十分钟在jb里面运行hibernate的】
here is an implementation of equals for a data-centric class. it demonstrates how different types of fields are treated: 【扩展信息:介绍下简单模版eastm 】
example
... 下一页