ºìÁª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. 71.234.128.* ÓÚ 2007-06-19 23:49:34·¢±í:

    7 card stud http://www.freewebs.com/casino-44/card-stud-3.html 7 card stud money tree payday loan money tree payday loan http://www.weddingwebsonline.com/goodman/guestbook/_disc17/00000207.htm casino baccarat http://www.freewebs.com/casino-97/baccarat-4.html baccarat online payday loan company payday loan company http://www.blenusmodels.com/_disc1/000004e3.htm ameriloan payday loan ameriloan payday loan http://www.scagliotti.net/_disc1/000019eb.htm

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

    easy payday loan http://www.superslamcountdown.com/_Hunting_Alaska/0000004b.htm easy payday loan cheap payday loan http://www.dgfoutdoors.com/_disc1/00000258.htm cheap payday loan online baccarat baccarat game http://www.freewebs.com/casino-66/baccarat-1.html 7 card stud 7 card stud http://www.freewebs.com/casino-82/card-stud-4.html payday advance loan payday advance loan http://www.labarbabietola.it/forum/forum/000009f5.htm

  3. 75.17.198.* ÓÚ 2007-06-19 19:15:43·¢±í:

    Dear online poker site

  4. 208.127.109.* ÓÚ 2007-06-19 07:42:56·¢±í:

    The main thing about texas holdem poker online

  5. 61.90.248.* ÓÚ 2007-06-19 04:41:07·¢±í:

    Begin with free online poker

  6. 24.2.16.* ÓÚ 2007-06-19 04:39:33·¢±í:

    Yet free party poker

  7. 0.0.0.* ÓÚ 2007-06-19 04:34:05·¢±í:

    Come best online poker room

  8. 68.8.37.* ÓÚ 2007-06-19 03:42:10·¢±í:

    How free online texas hold em

  9. 0.0.0.* ÓÚ 2007-06-19 03:40:49·¢±í:

    That is the online video poker game

  10. 82.79.46.* ÓÚ 2007-06-19 03:37:55·¢±í:

    A online video poker

  11. 222.66.48.* ÓÚ 2007-06-19 03:30:22·¢±í:

    As mentioned poker video

  12. 70.113.89.* ÓÚ 2007-06-19 03:26:23·¢±í:

    But how to play texas hold em

  13. 123.192.22.* ÓÚ 2007-06-19 03:25:23·¢±í:

    Do free online video poker

  14. 64.83.197.* ÓÚ 2007-06-19 03:16:39·¢±í:

    In other words video poker

  15. 0.0.0.* ÓÚ 2007-06-19 03:13:52·¢±í:

    Each poker rooms

  16. 76.19.236.* ÓÚ 2007-06-19 03:05:26·¢±í:

    With the party poker bonus code

  17. 124.154.0.* ÓÚ 2007-06-19 02:52:11·¢±í:

    This pacific poker download

  18. 68.200.60.* ÓÚ 2007-06-19 02:48:52·¢±í:

    Avoid poker video

  19. 210.205.32.* ÓÚ 2007-06-19 02:47:38·¢±í:

    Rare free video poker

  20. 189.170.64.* ÓÚ 2007-06-19 02:10:04·¢±í:

    Take free video poker game

  21. 201.255.32.* ÓÚ 2007-06-19 01:31:40·¢±í:

    All the live online poker

  22. 86.7.91.* ÓÚ 2007-06-19 00:59:43·¢±í:

    It partypoker

  23. 61.109.65.* ÓÚ 2007-06-19 00:58:51·¢±í:

    Likewise party poker cheat

  24. 24.186.88.* ÓÚ 2007-06-19 00:46:13·¢±í:

    Shall texas hold em poker

  25. 172.187.187.* ÓÚ 2007-06-19 00:37:51·¢±í:

    Join party poker cheat

  26. 71.204.44.* ÓÚ 2007-06-19 00:35:49·¢±í:

    Get play free poker

  27. 76.171.232.* ÓÚ 2007-06-19 00:31:14·¢±í:

    Based on pacific poker download

  28. 68.81.141.* ÓÚ 2007-06-19 00:26:40·¢±í:

    Offer play video poker