ºìÁª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-28 05:32:17·¢±í:

    java blackjack java blackjack http://www.freewebs.com/casino-78/blackjack-4.html no faxing payday loan no faxing payday loan http://www.socraticsocietyuvalde.org/socraticforum/_disc1/00001ea3.htm craps online craps online http://www.freewebs.com/casino-44/craps-6.html bad debt loan payday bad debt loan payday http://www.amaranthys.com/_disc1/00002a12.htm internet roulette http://www.freewebs.com/casino-53/roulette-7.html roulette tip

  2. 0.0.0.* ÓÚ 2007-06-28 04:22:19·¢±í:

    play free slots play free slots http://www.freewebs.com/casino-76/slots-2.html http://www.freewebs.com/casino-45/craps-1.html play craps online free how to play craps http://www.polishnetwork.com/polishnetwork/_disc1/000034b4.htm low fee payday loan low fee payday loan sonic payday loan http://www.haime.com.ar/_disc1/00000dc8.htm sonic payday loan online craps http://www.freewebs.com/casino-44/craps-7.html online craps

  3. 89.156.54.* ÓÚ 2007-06-28 03:20:37·¢±í:

    cash til payday loan http://www.booksbyrule.com/discb1/_disc3/00000269.htm cash til payday loan http://applesoranges.us/videos/_disc22/000000d9.htm payday cash advance payday cash advance easy payday loan easy payday loan http://www.ispor.org/disc/_disc/00000f0e.htm http://www.freewebs.com/casino-66/casino-7.html casino slots casino slots http://home.nc.rr.com/marktrail/_disc1/0000009c.htm loan till payday loan till payday

  4. 0.0.0.* ÓÚ 2007-06-28 02:25:29·¢±í:

    online roulette poker http://www.freewebs.com/casino-24/roulette-2.html online roulette poker http://www.vereinsring-liederbach.de/_disc1/00000010.htm cash advance payday loan cash advance payday loan online casino blackjack free blackjack http://www.freewebs.com/casino-53/blackjack-2.html cash til payday loan cash til payday loan http://applesoranges.us/videos/_disc22/000000db.htm low fee payday loan low fee payday loan http://www.bong-hit.com/_BongBuzz/00000dc1.htm

  5. 123.49.32.* ÓÚ 2007-06-28 01:47:54·¢±í:

    play craps rules of craps http://www.freewebs.com/casino-53/craps-7.html http://enska.khi.is/ragnheidur/_disc17/00004bcc.htm cash advance paydayloans cash advance paydayloans best online casino best online casino http://www.freewebs.com/casino-44/casino-7.html seven card stud http://www.freewebs.com/casino-53/card-stud-2.html 7 card stud cash til payday loan cash til payday loan http://www.barbaraboudion.com/_LT71DISCUSSION/0000006f.htm

  6. 0.0.0.* ÓÚ 2007-06-28 01:33:46·¢±í:

    http://www.perryg.com/TheOwlsPerch/mywebs2/chatroom1/_Hisdisc1/0000007e.htm payday cash loan payday cash loan instant faxless payday loan instant faxless payday loan http://www.chawner-web.co.uk/webb/_disc1/00002c05.htm payday loan 1000 http://www.business-bytes.com/forum/_disc2/00002637.htm payday loan 1000 blackjack online play blackjack http://www.freewebs.com/casino-78/blackjack-2.html cash until payday loan cash until payday loan http://www.goodfelloweb.com/cottage/_disc1/00000c38.htm

  7. 0.0.0.* ÓÚ 2007-06-28 00:22:49·¢±í:

    http://www.irishsetterclub.org/_Katrina_Disc/00000039.htm instant payday loan instant payday loan http://www.chawner-web.co.uk/webb/_disc1/00002bfe.htm faxless payday loan faxless payday loan craps table craps online http://www.freewebs.com/casino-44/craps-2.html http://www.freewebs.com/casino-97/slots-1.html progressive slots free online slots free online craps online craps http://www.freewebs.com/casino-13/craps-1.html

  8. 0.0.0.* ÓÚ 2007-06-28 00:22:19·¢±í:

    http://www.irishsetterclub.org/_Katrina_Disc/00000039.htm instant payday loan instant payday loan http://www.chawner-web.co.uk/webb/_disc1/00002bfe.htm faxless payday loan faxless payday loan craps table craps online http://www.freewebs.com/casino-44/craps-2.html http://www.freewebs.com/casino-97/slots-1.html progressive slots free online slots free online craps online craps http://www.freewebs.com/casino-13/craps-1.html

  9. 0.0.0.* ÓÚ 2007-06-27 23:56:50·¢±í:

    cash until payday loan cash until payday loan http://donne.dconline.info/forum/_disc27/0000042f.htm free casino games free casino games http://www.freewebs.com/casino-44/casino-6.html payday loan canada payday loan canada http://www.gotobaker.com/_job1disc/0000009d.htm casino poker free casino game http://www.freewebs.com/casino-76/casino-4.html http://www.cahunger.com/_disc1/000007db.htm no faxing payday loan no faxing payday loan

  10. 24.111.51.* ÓÚ 2007-06-27 23:31:55·¢±í:

    http://www.freewebs.com/casino-44/baccarat-2.html baccarat online baccarat online http://www.thelazaruslives.com/Racism%20Discussion%20Forum/Racism/0000003e.htm easy payday loan easy payday loan blogspot.com fax loan no payday site blogspot.com fax loan no payday site http://www.dlef.co.uk/discform/_disc4/000004d6.htm http://www.freewebs.com/casino-89/craps-5.html rules of craps gambling craps blogspot.com fax loan no payday site http://www.complaign.com/_disc2/00000ed5.htm blogspot.com fax loan no payday site

  11. 0.0.0.* ÓÚ 2007-06-27 23:31:08·¢±í:

    http://www.freewebs.com/casino-44/baccarat-2.html baccarat online baccarat online http://www.thelazaruslives.com/Racism%20Discussion%20Forum/Racism/0000003e.htm easy payday loan easy payday loan blogspot.com fax loan no payday site blogspot.com fax loan no payday site http://www.dlef.co.uk/discform/_disc4/000004d6.htm http://www.freewebs.com/casino-89/craps-5.html rules of craps gambling craps blogspot.com fax loan no payday site http://www.complaign.com/_disc2/00000ed5.htm blogspot.com fax loan no payday site

  12. 202.164.165.* ÓÚ 2007-06-27 16:53:00·¢±í:

    free online keno free keno http://www.freewebs.com/casino-66/keno-2.html ez payday loan ez payday loan http://www.cmpatristate.com/myweb/discussions/00001467.htm free online keno http://www.freewebs.com/casino-53/keno-2.html online keno online casino craps casino craps http://www.freewebs.com/casino-87/craps-3.html no credit check payday loan no credit check payday loan http://colescrossing.net/_bulletinboard/00000138.htm

  13. 0.0.0.* ÓÚ 2007-06-27 16:44:30·¢±í:

    savings account payday loan http://www.4freshcoffee.com/_disc1/00000535.htm savings account payday loan http://www.northpulaski.com/mysite2/_disc2/00000277.htm cheap payday loan cheap payday loan payday loan store payday loan store http://www.bobtodaro.com/BTMUSIC/00000bdc.htm play baccarat play baccarat http://www.freewebs.com/casino-45/baccarat-6.html casino baccarat baccarat game http://www.freewebs.com/casino-24/baccarat-3.html

  14. 0.0.0.* ÓÚ 2007-06-27 16:42:55·¢±í:

    casino roulette http://www.freewebs.com/casino-44/roulette-2.html roulette game bad credit payday loan http://rlstaton.com/mymessageboard/_disc2/00001c41.htm bad credit payday loan play keno online play keno online http://www.freewebs.com/casino-31/keno-7.html payday loan personal payday loan personal http://www.essex.ac.uk/afm/ma-discuss/_afm-ma-discuss/00000a00.htm low fee payday loan http://www.bigbellyservices.com/_disc5/000006e5.htm low fee payday loan

  15. 0.0.0.* ÓÚ 2007-06-27 16:32:27·¢±í:

    no teletrack payday loan http://www.chaoselites.com/Discussion/000001d7.htm no teletrack payday loan http://www.bong-hit.com/_BongBuzz/00000dc3.htm quick payday loan quick payday loan payday advance loan http://www.azodchat.com/freshwaterclassifieds/_disc24/0000055d.htm payday advance loan http://www.arbor-inc.biz/_disc9/000000ec.htm no faxing payday loan no faxing payday loan craps game online craps http://www.freewebs.com/casino-53/craps-6.html

  16. 0.0.0.* ÓÚ 2007-06-27 16:30:17·¢±í:

    free slots machine http://www.freewebs.com/casino-66/slots-7.html sim slots baccarat online free baccarat http://www.freewebs.com/casino-53/baccarat-3.html craps rules http://www.freewebs.com/casino-13/craps-7.html free craps game online baccarat http://www.freewebs.com/casino-68/baccarat-7.html free online baccarat fast payday loan fast payday loan http://www.wildlocals.com/_disc1/00001252.htm

  17. 0.0.0.* ÓÚ 2007-06-27 16:09:14·¢±í:

    payday loan on line http://www.iaap-satellite.org/_disc1/0000111e.htm payday loan on line http://www.freewebs.com/casino-44/card-stud-2.html seven card stud seven card stud seven card stud http://www.freewebs.com/casino-76/card-stud-2.html seven card stud loan till payday loan till payday http://www.btpformationeure.fr/_disc1/00000047.htm loan til payday loan til payday http://www.copysoft.net/_disc1/00000d60.htm

  18. 0.0.0.* ÓÚ 2007-06-27 13:07:38·¢±í:

    Still play free poker

  19. 0.0.0.* ÓÚ 2007-06-27 12:44:51·¢±í:

    7 card stud seven card stud http://www.freewebs.com/casino-53/card-stud-4.html keno online internet keno http://www.freewebs.com/casino-97/keno-4.html payday loan company http://www.dylanryanmay.com/_disc1/00000053.htm payday loan company ameriloan payday loan ameriloan payday loan http://lincoln.k12.mi.us/Buildings/HS/bargardi/_disc1/000000bd.htm cash advance paydayloans http://www.lettuceisnotenough.com/_PhD/00006a4e.htm cash advance paydayloans

  20. 0.0.0.* ÓÚ 2007-06-27 12:36:55·¢±í:

    Here are play poker

  21. 67.81.235.* ÓÚ 2007-06-27 11:54:30·¢±í:

    Never how to play texas hold em

  22. 0.0.0.* ÓÚ 2007-06-27 11:49:45·¢±í:

    Of the record online poker rooms

  23. 61.148.97.* ÓÚ 2007-06-27 11:38:57·¢±í:

    Get started online casino poker

  24. 202.28.27.* ÓÚ 2007-06-27 11:06:41·¢±í:

    For online texas holdem

  25. 62.231.243.* ÓÚ 2007-06-27 07:18:11·¢±í:

    no teletrack payday loan no teletrack payday loan http://mycropthorne.co.uk/Forum/ParishPlan/0000005f.htm payday loan store http://www.marysvilleteachers.org/forum/_MUTAdiscussion/0000068a.htm payday loan store cheap payday loan cheap payday loan http://www.peru.no/Peru-Noruega/00001edd.htm play free slots machine slots game http://www.freewebs.com/casino-78/slots-1.html baccarat game baccarat online http://www.freewebs.com/casino-89/baccarat-2.html

  26. 210.51.10.* ÓÚ 2007-06-26 18:08:45·¢±í:

    Change free poker download

  27. 0.0.0.* ÓÚ 2007-06-26 18:01:53·¢±í:

    Let's begin pacific poker download

  28. 204.48.128.* ÓÚ 2007-06-26 17:55:02·¢±í:

    More party poker com

  29. 200.109.43.* ÓÚ 2007-06-26 17:14:01·¢±í:

    no faxing payday loan http://www.splurging.com/discussion3/_disc22/00000297.htm no faxing payday loan internet blackjack http://www.freewebs.com/casino-45/blackjack-5.html casino blackjack http://www.freewebs.com/casino-66/craps-4.html craps game how to play craps fax payday loan fax payday loan http://www.gaston.k12.nc.us/departments/dec/CECAS/00001bb0.htm free baccarat casino baccarat http://www.freewebs.com/casino-24/baccarat-4.html

  30. 0.0.0.* ÓÚ 2007-06-26 16:46:41·¢±í:

    http://www.freewebs.com/casino-89/keno-6.html video keno play keno online rules of craps http://www.freewebs.com/casino-97/craps-2.html craps game no faxing payday loan no faxing payday loan http://24-7christianministries.com/_24-7Ministriesdisc1/00000063.htm internet keno online keno http://www.freewebs.com/casino-87/keno-3.html http://www.freewebs.com/casino-24/blackjack-3.html online casino blackjack online casino blackjack

  31. 0.0.0.* ÓÚ 2007-06-26 16:18:20·¢±í:

    7 card stud http://www.freewebs.com/casino-13/card-stud-1.html seven card stud http://www.freewebs.com/casino-31/baccarat-4.html online baccarat baccarat online instant faxless payday loan http://www.savannahslim.com/talkingSpirits/_disc1/0000024d.htm instant faxless payday loan faxless payday loan http://www.banffcentre.ca/bnmi/bridges/forum02/_forum/00000216.htm faxless payday loan payday loan online payday loan online http://lincoln.k12.mi.us/Buildings/HS/bargardi/_disc1/000000bc.htm

  32. 24.255.209.* ÓÚ 2007-06-26 16:14:05·¢±í:

    cash until payday loan http://www.fourvalves.com/chat/00000072.htm cash until payday loan military payday loan military payday loan http://mchome.lincoln.ac.uk/subsite/Week4A/000000bb.htm cash til payday loan cash til payday loan http://www.btpformationeure.fr/_disc1/00000045.htm fast cash payday loan fast cash payday loan http://www.mamm-mariag.it/_disc12/000000fe.htm play keno online http://www.freewebs.com/casino-44/keno-3.html online keno

  33. 74.130.182.* ÓÚ 2007-06-26 16:07:17·¢±í:

    gambling craps casino craps http://www.freewebs.com/casino-89/craps-4.html 7 card stud http://www.freewebs.com/casino-97/card-stud-1.html seven card stud online casino slots online casino slots http://www.freewebs.com/casino-24/slots-3.html no credit check payday loan no credit check payday loan http://thetrainingclinic.com/_disc1/000008d2.htm instant payday loan instant payday loan http://www.topofmyhead.info/_discGodVoter/000021ca.htm

  34. 0.0.0.* ÓÚ 2007-06-26 15:51:52·¢±í:

    Never free party poker

  35. 0.0.0.* ÓÚ 2007-06-26 15:20:51·¢±í:

    Quite free video poker