ºìÁª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. 72.36.178.* ÓÚ 2006-11-29 13:29:30·¢±í:

    Nice site!
    My homepage | Cool site