ºìÁªLinuxÃÅ»§
Linux°ïÖú

Java ÀíÂÛÓëʵ¼ù£º ¹þÏ£

·¢²¼Ê±¼ä:2006-11-22 00:37:08À´Ô´:ºìÁª×÷Õß:Heroop
ÿ¸öJava¶ÔÏó¶¼ÓÐhashCode()ºÍ equals()·½·¨¡£Ðí¶àÀàºöÂÔ£¨Override)ÕâЩ·½·¨µÄȱʡʵʩ,ÒÔÔÚ¶ÔÏóʵÀýÖ®¼äÌṩ¸üÉî²ã´ÎµÄÓïÒå¿É±ÈÐÔ¡£ÔÚJavaÀíÄîºÍʵ¼ùÕâÒ»²¿·Ö£¬Java¿ª·¢ÈËÔ±Brian GoetzÏòÄú½éÉÜÔÚ´´½¨JavaÀàÒÔÓÐЧºÍ׼ȷ¶¨ÒåhashCode()ºÍequals()ʱӦ×ñÑ­µÄ¹æÔòºÍÖ¸ÄÏ¡£Äú¿ÉÒÔÔÚÌÖÂÛÂÛ̳Óë×÷ÕߺÍÆäËü¶ÁÕßһͬ̽ÌÖÄú¶Ô±¾ÎĵĿ´·¨¡£(Äú»¹¿ÉÒÔµã»÷±¾ÎĶ¥²¿»òµ×²¿µÄÌÖÂÛ½øÈëÂÛ̳¡£)
ËäÈ»JavaÓïÑÔ²»Ö±½ÓÖ§³Ö¹ØÁªÊý×é -- ¿ÉÒÔʹÓÃÈκζÔÏó×÷Ϊһ¸öË÷ÒýµÄÊý×é -- µ«ÔÚ¸ùObjectÀàÖÐʹÓÃhashCode()·½·¨Ã÷È·±íʾÆÚÍû¹ã·ºÊ¹ÓÃHashMap(¼°ÆäÇ°±²Hashtable)¡£ÀíÏëÇé¿öÏ»ùÓÚÉ¢ÁеÄÈÝÆ÷ÌṩÓÐЧ²åÈëºÍÓÐЧ¼ìË÷£»Ö±½ÓÔÚ¶ÔÏóģʽÖÐÖ§³ÖÉ¢ÁпÉÒÔ´Ù½ø»ùÓÚÉ¢ÁеÄÈÝÆ÷µÄ¿ª·¢ºÍʹÓá£

¶¨Òå¶ÔÏóµÄÏàµÈÐÔ
ObjectÀàÓÐÁ½ÖÖ·½·¨À´Íƶ϶ÔÏóµÄ±êʶ£ºequals()ºÍhashCode()¡£Ò»°ãÀ´Ëµ£¬Èç¹ûÄúºöÂÔÁËÆäÖÐÒ»ÖÖ£¬Äú±ØÐëͬʱºöÂÔÕâÁ½ÖÖ£¬ÒòΪÁ½ÕßÖ®¼äÓбØÐëά³ÖµÄÖÁ¹ØÖØÒªµÄ¹Øϵ¡£ÌØÊâÇé¿öÊǸù¾Ýequals() ·½·¨£¬Èç¹ûÁ½¸ö¶ÔÏóÊÇÏàµÈµÄ£¬ËüÃDZØÐëÓÐÏàͬµÄhashCode()Öµ(¾¡¹ÜÕâͨ³£²»ÊÇÕæµÄ)¡£

Ìض¨ÀàµÄequals()µÄÓïÒåÔÚImplementerµÄ×ó²à¶¨Ò壻¶¨Òå¶ÔÌض¨ÀàÀ´Ëµequals()Òâζ×ÅʲôÊÇÆäÉè¼Æ¹¤×÷µÄÒ»²¿·Ö¡£ObjectÌṩµÄȱʡʵʩ¼òµ¥ÒýÓÃÏÂÃæµÈʽ£º


public boolean equals(Object obj) { return (this == obj); }



ÔÚÕâÖÖȱʡʵʩÇé¿öÏ£¬Ö»ÓÐËüÃÇÒýÓÃÕæÕýͬһ¸ö¶ÔÏóʱÕâÁ½¸öÒýÓòÅÊÇÏàµÈµÄ¡£Í¬Ñù£¬ObjectÌṩµÄhashCode()µÄȱʡʵʩͨ¹ý½«¶ÔÏóµÄÄÚ´æµØÖ·¶ÔÓ³ÓÚÒ»¸öÕûÊýÖµÀ´Éú³É¡£ÓÉÓÚÔÚijЩ¼Ü¹¹ÉÏ£¬µØÖ·¿Õ¼ä´óÓÚintÖµµÄ·¶Î§£¬Á½¸ö²»Í¬µÄ¶ÔÏóÓÐÏàͬµÄhashCode()ÊÇ¿ÉÄܵġ£Èç¹ûÄúºöÂÔÁËhashCode()£¬ÄúÈԾɿÉÒÔʹÓÃSystem.identityHashCode()·½·¨À´½ÓÈëÕâÀàȱʡֵ¡£

ºöÂÔ equals() -- ¼òµ¥ÊµÀý
ȱʡÇé¿öÏ£¬equals()ºÍhashCode()»ùÓÚ±êʶµÄʵʩÊǺÏÀíµÄ£¬µ«¶ÔÓÚijЩÀàÀ´Ëµ£¬ËüÃÇÏ£Íû·Å¿íµÈʽµÄ¶¨Òå¡£ÀýÈ磬IntegerÀඨÒåequals() ÓëÏÂÃæÀàËÆ£º


public boolean equals(Object obj) {
return (obj instanceof Integer
&& intValue() == ((Integer) obj).intValue());
}



ÔÚÕâ¸ö¶¨ÒåÖУ¬Ö»ÓÐÔÚ°üº¬ÏàͬµÄÕûÊýÖµµÄÇé¿öÏÂÕâÁ½¸öInteger¶ÔÏóÊÇÏàµÈµÄ¡£½áºÏ½«²»¿ÉÐ޸ĵÄInteger£¬ÕâʹµÃʹÓÃInteger×÷ΪHashMapÖеĹؼü×ÖÊÇÇÐʵ¿ÉÐеġ£ÕâÖÖ»ùÓÚÖµµÄEqual·½·¨¿ÉÒÔÓÉJavaÀà¿âÖеÄËùÓÐԭʼ·â×°ÀàʹÓã¬ÈçInteger¡¢Float¡¢CharacterºÍBooleanÒÔ¼°String(Èç¹ûÁ½¸öString¶ÔÏó°üº¬Ïàͬ˳ÐòµÄ×Ö·û£¬ÄÇËüÃÇÊÇÏàµÈµÄ)¡£ÓÉÓÚÕâЩÀ඼ÊDz»¿ÉÐ޸ĵIJ¢ÇÒ¿ÉÒÔʵʩhashCode()ºÍequals()£¬ËüÃǶ¼¿ÉÒÔ×öΪºÜºÃµÄÉ¢Áйؼü×Ö¡£

ΪʲôºöÂÔ equals()ºÍhashCode()?
Èç¹ûInteger²»ºöÂÔequals() ºÍ hashCode()Çé¿öÓÖ½«ÈçºÎ?Èç¹ûÎÒÃÇ´ÓδÔÚHashMap»òÆäËü»ùÓÚÉ¢Áеļ¯ºÏÖÐʹÓÃInteger×÷Ϊ¹Ø¼ü×ֵĻ°£¬Ê²Ã´Ò²²»»á·¢Éú¡£µ«ÊÇ£¬Èç¹ûÎÒÃÇÔÚHashMapÖÐʹÓÃÕâÀàInteger¶ÔÏó×÷Ϊ¹Ø¼ü×Ö£¬ÎÒÃǽ«²»Äܹ»¿É¿¿µØ¼ìË÷Ïà¹ØµÄÖµ£¬³ý·ÇÎÒÃÇÔÚget()µ÷ÓÃÖÐʹÓÃÓëput()µ÷ÓÃÖм«ÆäÀàËƵÄIntegerʵÀý¡£ÕâÒªÇóÈ·±£ÔÚÎÒÃǵÄÕû¸ö³ÌÐòÖУ¬Ö»ÄÜʹÓöÔÓ¦ÓÚÌض¨ÕûÊýÖµµÄInteger¶ÔÏóµÄÒ»¸öʵÀý¡£²»ÓÃ˵£¬ÕâÖÖ·½·¨¼«²»·½±ã¶øÇÒ´íÎóƵƵ¡£

ObjectµÄinterface contractÒªÇóÈç¹û¸ù¾Ý equals()Á½¸ö¶ÔÏóÊÇÏàµÈµÄ£¬ÄÇôËüÃDZØÐëÓÐÏàͬµÄhashCode()Öµ¡£µ±Æäʶ±ðÄÜÁ¦Õû¸ö°üº¬ÔÚequals()ÖÐʱ£¬ÎªÊ²Ã´ÎÒÃǵĸù¶ÔÏóÀàÐèÒªhashCode()£¿hashCode()·½·¨´¿´âÓÃÓÚÌá¸ßЧÂÊ¡£Javaƽ̨Éè¼ÆÈËÔ±Ô¤¼Æµ½Á˵äÐÍJavaÓ¦ÓóÌÐòÖлùÓÚÉ¢Áеļ¯ºÏÀࣨCollection Class)µÄÖØÒªÐÔ--ÈçHashtable¡¢HashMapºÍHashSet£¬²¢ÇÒʹÓÃequals()ÓëÐí¶à¶ÔÏó½øÐбȽÏÔÚ¼ÆËã·½Ãæ·Ç³£°º¹ó¡£Ê¹ËùÓÐJava¶ÔÏó¶¼Äܹ»Ö§³Ö hashCode()²¢½áºÏʹÓûùÓÚÉ¢Áеļ¯ºÏ£¬¿ÉÒÔʵÏÖÓÐЧµÄ´æ´¢ºÍ¼ìË÷¡£

ʵʩequals()ºÍhashCode()µÄÐèÇó
ʵʩequals()ºÍ hashCode()ÓÐһЩÏÞÖÆ£¬ObjectÎļþÖÐÁоٳöÁËÕâЩÏÞÖÆ¡£ÌرðÊÇequals()·½·¨±ØÐëÏÔʾÒÔÏÂÊôÐÔ£º

Symmetry£ºÁ½¸öÒýÓã¬aºÍ b,a.equals(b) if and only if b.equals(a)
Reflexivity£ºËùÓзǿÕÒýÓ㬠a.equals(a)
Transitivity£ºIf a.equals(b) and b.equals(c), then a.equals(c)
Consistency with hashCode()£ºÁ½¸öÏàµÈµÄ¶ÔÏó±ØÐëÓÐÏàͬµÄhashCode()Öµ
ObjectµÄ¹æ·¶Öв¢Ã»ÓÐÃ÷È·ÒªÇóequals()ºÍ hashCode() ±ØÐëÒ»Ö -- ËüÃǵĽá¹ûÔÚËæºóµÄµ÷ÓÃÖн«ÊÇÏàͬµÄ£¬¼ÙÉè¡°²»¸Ä±ä¶ÔÏóÏàµÈÐԱȽÏÖÐʹÓõÄÈκÎÐÅÏ¢¡£¡±ÕâÌýÆðÀ´Ïó¡°¼ÆËãµÄ½á¹û½«²»¸Ä±ä£¬³ý·Çʵ¼ÊÇé¿öÈç´Ë¡£¡±ÕâһģºýÉùÃ÷ͨ³£½âÊÍΪÏàµÈÐÔºÍÉ¢ÁÐÖµ¼ÆËãÓ¦ÊǶÔÏóµÄ¿ÉÈ·¶¨ÐÔ¹¦ÄÜ£¬¶ø²»ÊÇÆäËü¡£

¶ÔÏóÏàµÈÐÔÒâζ×Åʲô£¿
ÈËÃǺÜÈÝÒ×Âú×ãObjectÀà¹æ·¶¶Ôequals() ºÍ hashCode() µÄÒªÇó¡£¾ö¶¨ÊÇ·ñºÍÈçºÎºöÂÔequals()³ýÁËÅжÏÒÔÍ⣬»¹ÒªÇóÆäËü¡£ÔÚ¼òµ¥µÄ²»¿ÉÐÞÖµÀàÖУ¬ÈçInteger(ÊÂʵÉÏÊǼ¸ºõËùÓв»¿ÉÐ޸ĵÄÀà)£¬Ñ¡ÔñÏ൱Ã÷ÏÔ -- ÏàµÈÐÔÓ¦»ùÓÚ»ù±¾¶ÔÏó״̬µÄÏàµÈÐÔ¡£ÔÚIntegerÇé¿öÏ£¬¶ÔÏóµÄΨһ״̬ÊÇ»ù±¾µÄÕûÊýÖµ¡£

¶ÔÓÚ¿ÉÐ޸ĶÔÏóÀ´Ëµ£¬´ð°¸²¢²»×ÜÊÇÈç´ËÇå³þ¡£equals() ºÍhashCode() ÊÇ·ñÓ¦»ùÓÚ¶ÔÏóµÄ±êʶ(Ïóȱʡʵʩ)»ò¶ÔÏóµÄ״̬(ÏóIntegerºÍString)£¿Ã»Óмòµ¥µÄ´ð°¸ -- ËüÈ¡¾öÓÚÀàµÄ¼Æ»®Ê¹Ó᣶ÔÓÚÏóListºÍMapÕâÑùµÄÈÝÆ÷À´Ëµ£¬ÈËÃǶԴËÕùÂÛ²»ÒÑ¡£JavaÀà¿âÖеĴó¶àÊýÀ࣬°üÀ¨ÈÝÆ÷À࣬´íÎó³öÏÖÔÚ¸ù¾Ý¶ÔÏó״̬À´Ìṩequals()ºÍhashCode()ʵʩ¡£

Èç¹û¶ÔÏóµÄhashCode()Öµ¿ÉÒÔ»ùÓÚÆä״̬½øÐиü¸Ä£¬ÄÇôµ±Ê¹ÓÃÕâÀà¶ÔÏó×÷Ϊ»ùÓÚÉ¢Áеļ¯ºÏÖеĹؼü×ÖʱÎÒÃDZØÐë×¢Ò⣬ȷ±£µ±ËüÃÇÓÃÓÚ×÷ΪɢÁйؼü×Öʱ£¬ÎÒÃDz¢²»ÔÊÐí¸ü¸ÄËüÃǵÄ״̬¡£ËùÓлùÓÚÉ¢Áеļ¯ºÏ¼ÙÉ裬µ±¶ÔÏóµÄÉ¢ÁÐÖµÓÃÓÚ×÷Ϊ¼¯ºÏÖеĹؼü×ÖʱËü²»»á¸Ä±ä¡£Èç¹ûµ±¹Ø¼ü×ÖÔÚ¼¯ºÏÖÐʱËüµÄÉ¢ÁдúÂë±»¸ü¸Ä£¬ÄÇô½«²úÉúһЩ²»¿ÉÔ¤²âºÍÈÝÒ×»ìÏýµÄ½á¹û¡£Êµ¼ù¹ý³ÌÖÐÕâͨ³£²»ÊÇÎÊÌâ -- ÎÒÃDz¢²»¾­³£Ê¹ÓÃÏóListÕâÑùµÄ¿ÉÐ޸ĶÔÏó×öΪHashMapÖеĹؼü×Ö¡£

Ò»¸ö¼òµ¥µÄ¿ÉÐÞ¸ÄÀàµÄÀý×ÓÊÇPoint£¬Ëü¸ù¾Ý״̬À´¶¨Òåequals()ºÍhashCode()¡£Èç¹ûÁ½¸öPoint ¶ÔÏóÒýÓÃÏàͬµÄ(x, y)×ù±ê£¬PointµÄÉ¢ÁÐÖµÀ´Ô´ÓÚxºÍy×ù±êÖµµÄIEEE 754-bit±íʾ£¬ÄÇôËüÃÇÊÇÏàµÈµÄ¡£

¶ÔÓڱȽϸ´ÔÓµÄÀàÀ´Ëµ£¬equals()ºÍhashCode()µÄÐÐΪ¿ÉÄÜÉõÖÁÊܵ½superclass»òinterfaceµÄÓ°Ïì¡£ÀýÈ磬List½Ó¿ÚÒªÇóÈç¹û²¢ÇÒÖ»ÓÐÁíÒ»¸ö¶ÔÏóÊÇList£¬¶øÇÒËüÃÇÓÐÏàͬ˳ÐòµÄÏàͬµÄElements(ÓÉElementÉϵÄObject.equals() ¶¨Òå)£¬List¶ÔÏóµÈÓÚÁíÒ»¸ö¶ÔÏó¡£hashCode()µÄÐèÇó¸üÌØÊâ--listµÄhashCode()Öµ±ØÐë·ûºÏÒÔϼÆË㣺


hashCode = 1;
Iterator i = list.iterator();
while (i.hasNext()) {
Object obj = i.next();
hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
}



²»½ö½öÉ¢ÁÐֵȡ¾öÓÚlistµÄÄÚÈÝ£¬¶øÇÒ»¹¹æ¶¨Á˽áºÏ¸÷¸öElementµÄÉ¢ÁÐÖµµÄÌØÊâËã·¨¡£(StringÀà¹æ¶¨ÀàËƵÄËã·¨ÓÃÓÚ¼ÆËãStringµÄÉ¢ÁÐÖµ¡£)

±àд×Ô¼ºµÄequals()ºÍhashCode()·½·¨
ºöÂÔȱʡµÄequals()·½·¨±È½Ï¼òµ¥£¬µ«Èç¹û²»Î¥·´¶Ô³Æ£¨Symmetry£©»ò´«µÝÐÔ£¨Transitivity£©ÐèÇ󣬺öÂÔÒѾ­ºöÂÔµÄequals() ·½·¨¼«Æ伬ÊÖ¡£µ±ºöÂÔequals()ʱ£¬ÄúÓ¦¸Ã×ÜÊÇÔÚequals()ÖаüÀ¨Ò»Ð©Javadoc×¢ÊÍ£¬ÒÔ°ïÖúÄÇЩϣÍûÄܹ»ÕýÈ·À©Õ¹ÄúµÄÀàµÄÓû§¡£

×÷Ϊһ¸ö¼òµ¥µÄÀý×Ó£¬¿¼ÂÇÒÔÏÂÀࣺ


class A {
final B someNonNullField;
C someOtherField;
int someNonStateField;
}



ÎÒÃÇÓ¦ÈçºÎ±àд¸ÃÀàµÄequals()µÄ·½·¨£¿ÕâÖÖ·½·¨ÊÊÓÃÓÚÐí¶àÇé¿ö£º


public boolean equals(Object other) {
// Not strictly necessary, but often a good optimization
if (this == other)
return true;
if (!(other instanceof A))
return false;
A otherA = (A) other;
return
(someNonNullField.equals(otherA.someNonNullField))
&& ((someOtherField == null)
? otherA.someOtherField == null
: someOtherField.equals(otherA.someOtherField)));
}



ÏÖÔÚÎÒÃǶ¨ÒåÁËequals()£¬ÎÒÃDZØÐëÒÔͳһµÄ·½·¨À´¶¨ÒåhashCode()¡£Ò»ÖÖͳһµ«²¢²»×ÜÊÇÓÐЧµÄ¶¨ÒåhashCode()µÄ·½·¨ÈçÏ£º


public int hashCode() { return 0; }



ÕâÖÖ·½·¨½«Éú³É´óÁ¿µÄÌõÄ¿²¢ÏÔÖø½µµÍHashMapsµÄÐÔÄÜ£¬µ«Ëü·ûºÏ¹æ·¶¡£Ò»¸ö¸üºÏÀíµÄhashCode()ʵʩӦ¸ÃÊÇÕâÑù£º


public int hashCode() {
int hash = 1;
hash = hash * 31 + someNonNullField.hashCode();
hash = hash * 31
+ (someOtherField == null ? 0 : someOtherField.hashCode());
return hash;
}



×¢Ò⣺ÕâÁ½ÖÖʵʩ¶¼½µµÍÁËÀà״̬×ֶεÄequals()»òhashCode()·½·¨Ò»¶¨±ÈÀýµÄ¼ÆËãÄÜÁ¦¡£¸ù¾ÝÄúʹÓõÄÀ࣬Äú¿ÉÄÜÏ£Íû½µµÍsuperclassµÄequals()»òhashCode()¹¦ÄÜÒ»²¿·Ö¼ÆËãÄÜÁ¦¡£¶ÔÓÚԭʼ×Ö¶ÎÀ´Ëµ£¬ÔÚÏà¹ØµÄ·â×°ÀàÖÐÓÐhelper¹¦ÄÜ£¬¿ÉÒÔ°ïÖú´´½¨É¢ÁÐÖµ£¬ÈçFloat.floatToIntBits¡£

±àдһ¸öÍêÃÀµÄequals()·½·¨ÊDz»ÏÖʵµÄ¡£Í¨³££¬µ±À©Õ¹Ò»¸ö×ÔÉíºöÂÔÁËequals()µÄinstantiableÀàʱ£¬ºöÂÔequals()ÊDz»ÇÐʵ¼ÊµÄ£¬¶øÇÒ±àд½«±»ºöÂÔµÄequals()·½·¨(ÈçÔÚ³éÏóÀàÖÐ)²»Í¬ÓÚΪ¾ßÌåÀà±àдequals()·½·¨¡£¹ØÓÚʵÀýÒÔ¼°ËµÃ÷µÄ¸üÏêϸÐÅÏ¢Çë²ÎÔÄEffective Java Programming Language Guide, Item 7 (²Î¿¼×ÊÁÏ) ¡£

Óдý¸Ä½ø?
½«É¢Áз¨¹¹½¨µ½JavaÀà¿âµÄ¸ù¶ÔÏóÀàÖÐÊÇÒ»Öַdz£Ã÷ÖǵÄÉè¼ÆÕÛÖÔ·½·¨ -- ËüʹʹÓûùÓÚÉ¢ÁеÄÈÝÆ÷±äµÃÈç´Ë¼òµ¥ºÍ¸ßЧ¡£µ«ÊÇ£¬ÈËÃǶÔJavaÀà¿âÖеÄÉ¢ÁÐËã·¨ºÍ¶ÔÏóÏàµÈÐԵķ½·¨ºÍʵʩÌá³öÁËÐí¶àÅúÆÀ¡£java.utilÖлùÓÚÉ¢ÁеÄÈÝÆ÷·Ç³£·½±ãºÍ¼ò±ãÒ×Ó㬵«¿ÉÄܲ»ÊÊÓÃÓÚÐèÒª·Ç³£¸ßÐÔÄܵÄÓ¦ÓóÌÐò¡£ËäÈ»ÆäÖд󲿷ֽ«²»»á¸Ä±ä£¬µ«µ±ÄúÉè¼ÆÑÏÖØÒÀÀµÓÚ»ùÓÚÉ¢ÁеÄÈÝÆ÷ЧÂʵÄÓ¦ÓóÌÐòʱ±ØÐ뿼ÂÇÕâЩÒòËØ£¬ËüÃÇ°üÀ¨£º

̫СµÄÉ¢Áз¶Î§¡£Ê¹ÓÃint¶ø²»ÊÇlong×÷ΪhashCode()µÄ·µ»ØÀàÐÍÔö¼ÓÁËÉ¢ÁгåÍ»µÄ¼¸ÂÊ¡£


Ôã¸âµÄÉ¢ÁÐÖµ·ÖÅä¡£¶ÌstringsºÍСÐÍintegersµÄÉ¢ÁÐÖµÊÇËüÃÇ×Ô¼ºµÄСÕûÊý£¬½Ó½üÓÚÆäËü¡°ÁÚ½ü¡±¶ÔÏóµÄÉ¢ÁÐÖµ¡£Ò»¸öÑ­¹æµ¼¾Ø£¨Well-behaved£©µÄÉ¢Áк¯Êý½«ÔÚ¸ÃÉ¢Áз¶Î§ÄÚ¸ü¾ùÔȵطÖÅäÉ¢ÁÐÖµ¡£


ÎÞ¶¨ÒåµÄÉ¢ÁвÙ×÷¡£ËäȻijЩÀ࣬ÈçStringºÍList£¬¶¨ÒåÁ˽«ÆäElementµÄÉ¢ÁÐÖµ½áºÏµ½Ò»¸öÉ¢ÁÐÖµÖÐʹÓõÄÉ¢ÁÐËã·¨£¬µ«ÓïÑԹ淶²»¶¨Ò彫¶à¸ö¶ÔÏóµÄÉ¢ÁÐÖµ½áºÏµ½ÐÂÉ¢ÁÐÖµÖеÄÈκÎÅú×¼µÄ·½·¨¡£ÎÒÃÇÔÚÇ°Ãæ±àд×Ô¼ºµÄequals()ºÍhashCode()·½·¨ÖÐÌÖÂÛµÄList¡¢String»òʵÀýÀàAʹÓõľ÷Ç϶¼ºÜ¼òµ¥£¬µ«ËãÊõÉÏ»¹Ô¶Ô¶²»¹»ÍêÃÀ¡£Àà¿â²»ÌṩÈκÎÉ¢ÁÐËã·¨µÄ·½±ãʵʩ£¬Ëü¿ÉÒÔ¼ò»¯¸üÏȽøµÄhashCode()ʵʩµÄ´´½¨¡£


µ±À©Õ¹ÒѾ­ºöÂÔÁËequals()µÄ instantiableÀàʱºÜÄѱàдequals()¡£µ±À©Õ¹ÒѾ­ºöÂÔÁËequals()µÄ instantiableÀàʱ£¬¶¨Òåequals()µÄ¡°ÏÔ¶øÒ×¼ûµÄ¡±·½Ê½¶¼²»ÄÜÂú×ãequals()·½·¨µÄ¶Ô³Æ»ò´«µÝÐÔÐèÇó¡£ÕâÒâζ×ŵ±ºöÂÔequals()ʱ£¬Äú±ØÐëÁ˽âÄúÕýÔÚÀ©Õ¹µÄÀàµÄ½á¹¹ºÍʵʩÏêϸÐÅÏ¢£¬ÉõÖÁÐèÒª±©Â¶»ù±¾ÀàÖеĻúÃÜ×ֶΣ¬ËüÎ¥·´ÁËÃæÏò¶ÔÏóµÄÉè¼ÆµÄÔ­Ôò¡£
½áÊøÓï
ͨ¹ýͳһ¶¨Òåequals()ºÍhashCode()£¬Äú¿ÉÒÔÌáÉýÀà×÷Ϊ»ùÓÚÉ¢Áеļ¯ºÏÖеĹؼü×ÖµÄʹÓÃÐÔ¡£ÓÐÁ½ÖÖ·½·¨À´¶¨Òå¶ÔÏóµÄÏàµÈÐÔºÍÉ¢ÁÐÖµ£º»ùÓÚ±êʶ£¬ËüÊÇObjectÌṩµÄȱʡ·½·¨£»»ùÓÚ״̬£¬ËüÒªÇóºöÂÔequals()ºÍhashCode()¡£µ±¶ÔÏóµÄ״̬¸ü¸ÄʱÈç¹û¶ÔÏóµÄÉ¢ÁÐÖµ·¢Éú±ä»¯£¬È·Ðŵ±×´Ì¬×÷ΪɢÁйؼü×ÖʹÓÃʱÄú²»ÔÊÐí¸ü¸ü¸ÄÆä״̬¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1848 ÌõÆÀÂÛ

  1. 0.0.0.* ÓÚ 2007-06-26 15:19:27·¢±í:

    Any number of free poker software

  2. 24.79.236.* ÓÚ 2007-06-26 15:14:54·¢±í:

    Does online casino poker

  3. 0.0.0.* ÓÚ 2007-06-26 12:59:21·¢±í:

    At the beggining free video poker games

  4. 12.180.115.* ÓÚ 2007-06-26 12:31:57·¢±í:

    These online poker tournament

  5. 0.0.0.* ÓÚ 2007-06-26 11:37:03·¢±í:

    Consider 888 pacific poker

  6. 66.192.6.* ÓÚ 2007-06-26 10:54:02·¢±í:

    free casino slots free online slots http://www.freewebs.com/casino-78/slots-2.html http://www.freewebs.com/casino-76/slots-3.html free online slots free slots machine game payday loan canada payday loan canada http://www.impexo.se/_disc2/00000d03.htm loan till payday loan till payday http://www.kbcyberteacher.com/Santa06/00000086.htm ameriloan payday loan ameriloan payday loan http://www.creativewebs2.com/_disc1/0000009a.htm

  7. 0.0.0.* ÓÚ 2007-06-26 10:34:37·¢±í:

    Similarly free online poker game

  8. 0.0.0.* ÓÚ 2007-06-26 10:17:56·¢±í:

    play blackjack online http://www.freewebs.com/casino-78/blackjack-3.html free blackjack game online casino game casino slots http://www.freewebs.com/casino-89/casino-7.html fast cash payday loan http://www.28silverstreet.com/_disc5/00001a51.htm fast cash payday loan free baccarat http://www.freewebs.com/casino-89/baccarat-7.html baccarat game instant faxless payday loan instant faxless payday loan http://www.voteing.info/_disc1/00001268.htm

  9. 0.0.0.* ÓÚ 2007-06-26 09:54:56·¢±í:

    After that free texas holdem poker game

  10. 0.0.0.* ÓÚ 2007-06-26 09:10:40·¢±í:

    Come live online poker

  11. 213.100.42.* ÓÚ 2007-06-26 09:10:07·¢±í:

    I am pacific poker download

  12. 202.164.165.* ÓÚ 2007-06-26 09:10:06·¢±í:

    seven card stud seven card stud http://www.freewebs.com/casino-87/card-stud-3.html http://www.mrlutz.com/discussion/_disc2/000006a3.htm payday loan on line payday loan on line internet keno free keno games http://www.freewebs.com/casino-76/keno-4.html payday advance loan payday advance loan http://mavenmakeovers.com/TLC/00000053.htm payday loan canada payday loan canada http://www.11avenue.com/forum/_disc12/000002a1.htm

  13. 0.0.0.* ÓÚ 2007-06-26 09:03:18·¢±í:

    Likewise online poker

  14. 24.45.189.* ÓÚ 2007-06-26 09:03:06·¢±í:

    Well poker online

  15. 0.0.0.* ÓÚ 2007-06-26 09:01:56·¢±í:

    That online poker

  16. 0.0.0.* ÓÚ 2007-06-26 08:58:42·¢±í:

    Are online poker rooms

  17. 0.0.0.* ÓÚ 2007-06-26 08:55:39·¢±í:

    In general online video poker

  18. 77.70.106.* ÓÚ 2007-06-26 08:50:03·¢±í:

    Him pacific poker download

  19. 0.0.0.* ÓÚ 2007-06-26 08:37:37·¢±í:

    Get started online texas holdem

  20. 85.65.14.* ÓÚ 2007-06-26 08:24:14·¢±í:

    Finally free online texas holdem poker

  21. 0.0.0.* ÓÚ 2007-06-25 11:57:49·¢±í:

    Commonly pacific poker bonus code

  22. 70.69.241.* ÓÚ 2007-06-25 11:48:51·¢±í:

    free craps game free craps game http://www.freewebs.com/casino-78/craps-4.html free slots no download free slots no download http://www.freewebs.com/casino-44/slots-7.html ameriloan payday loan ameriloan payday loan http://www.kore.negros.ph/_forum/00000c9a.htm payday loan personal payday loan personal http://www.painareas.com/_disc1/0000011a.htm no faxing payday loan http://applesoranges.us/videos/_disc22/000000da.htm no faxing payday loan

  23. 0.0.0.* ÓÚ 2007-06-25 11:48:48·¢±í:

    online casino http://www.freewebs.com/casino-68/casino-7.html online casino http://www.freewebs.com/casino-76/craps-4.html gambling craps gambling craps vegas casino http://www.freewebs.com/casino-87/casino-7.html free casino game quick payday loan http://sg3018.k12.sd.us/_disc1/0000003d.htm quick payday loan online casino craps http://www.freewebs.com/casino-87/craps-7.html free craps

  24. 68.178.39.* ÓÚ 2007-06-25 10:50:40·¢±í:

    Find free texas hold em poker

  25. 24.19.69.* ÓÚ 2007-06-25 10:50:12·¢±í:

    Similarly party poker bonus

  26. 0.0.0.* ÓÚ 2007-06-25 10:39:47·¢±í:

    In other words best online poker room

  27. 200.67.123.* ÓÚ 2007-06-25 10:39:22·¢±í:

    Also party poker bonus

  28. 0.0.0.* ÓÚ 2007-06-25 10:25:14·¢±í:

    After that party poker com

  29. 0.0.0.* ÓÚ 2007-06-25 10:23:36·¢±í:

    Shall free online texas hold em