日期:2014-05-16  浏览次数:20682 次

apache 中的EqualsBuilder和HashCodeBuilder

自动化hashCode()和equals()
? 问题产生:当需要自动实现hashCode()和equals()方法
? 解决方法:使用EqualsBuilder和HashCodeBuilder
?
? 使用举例:
?

?import??org.apache.commons.lang.builder.HashCodeBuilder;
?import?
?org.apache.commons.lang.builder.EqualsBuilder;

?public???class??PoliticalCandidate??
{
????
?//?
?Member?variables?-?omitted?for?brevity
????
?//?
?Constructors?-?omitted?for?brevity
????
?//?
?get/set?methods?-?omitted?for?brevity
????
?//??A?hashCode?which?creates?a?hash?from?the?two?unique?identifiers?

?

??????
public???int??hashCode(?)??{
????????
?return???new??HashCodeBuilder(?17?,??37?
)
???????????????????????.append(firstName)
???????????????????????.append(lastName).toHashCode(?);
????}
?

?