javaÖÐhashcode()ºÍequals()µÄÏê½â(ת)
ÔÌùµØÖ·http://www.javaeye.com/topic/257191
½ñÌìÏÂÎçÑо¿ÁË°ëÌìhashcode()ºÍequals()·½·¨£¬ÖÕÓÚÓÐÁËÒ»µãµãµÄÃ÷°×£¬Ð´ÏÂÀ´Óë´ó¼Ò·ÖÏí£¨zhaoxudong
2008.10.23Íí21.36£©¡£
1. Ê×ÏÈequals()ºÍhashcode()ÕâÁ½¸ö·½·¨¶¼ÊÇ´ÓobjectÀàÖм̳йýÀ´µÄ¡£
equals()·½·¨ÔÚobjectÀàÖж¨ÒåÈçÏ£º
public boolean equals(Object obj) {
return (this == obj);
}
ºÜÃ÷ÏÔÊǶÔÁ½¸ö¶ÔÏóµÄµØÖ·Öµ½øÐеıȽϣ¨¼´±È½ÏÒýÓÃÊÇ·ñÏàͬ£©¡£µ«ÊÇÎÒÃDZØÐèÇå³þ£¬µ±String
¡¢Math¡¢»¹ÓÐInteger¡¢Double¡£¡£¡£¡£µÈÕâЩ·â×°ÀàÔÚʹÓÃequals()·½·¨Ê±£¬ÒѾ¸²¸ÇÁËobjectÀàµÄequals£¨£©·½·¨¡£±È
ÈçÔÚStringÀàÖÐÈçÏ£º
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
ºÜÃ÷ÏÔ£¬ÕâÊǽøÐеÄÄÚÈݱȽϣ¬¶øÒѾ²»ÔÙÊǵØÖ·µÄ±È½Ï¡£ÒÀ´ÎÀàÍÆDouble¡¢Integer¡¢Math¡£¡£¡£¡£µÈµÈÕâЩÀ඼ÊÇÖØдÁË
equals()·½·¨µÄ£¬´Ó¶ø½øÐеÄÊÇÄÚÈݵıȽϡ£µ±È»ÁË»ù±¾ÀàÐÍÊǽøÐÐÖµµÄ±È½Ï£¬Õâ¸öûÓÐʲôºÃ˵µÄ¡£
ÎÒÃÇ»¹Ó¦¸Ã×¢Ò⣬JavaÓïÑÔ¶Ôequals()µÄÒªÇóÈçÏ£¬ÕâЩҪÇóÊDZØÐë×ñѵģº
• ¶Ô³ÆÐÔ£ºÈç¹ûx.equals(y)·µ»ØÊÇ“true”£¬ÄÇôy.equals(x)Ò²Ó¦¸Ã·µ»ØÊÇ“true”¡£
• ·´ÉäÐÔ£ºx.equals(x)±ØÐë·µ»ØÊÇ“true”¡£
•
ÀàÍÆÐÔ£ºÈç¹ûx.equals(y)·µ»ØÊÇ“true”£¬¶øÇÒy.equals(z)·µ»ØÊÇ“true”£¬ÄÇôz.equals(x)Ò²Ó¦¸Ã·µ»ØÊÇ
“true”¡£
•
»¹ÓÐÒ»ÖÂÐÔ£ºÈç¹ûx.equals(y)·µ»ØÊÇ“true”£¬Ö»ÒªxºÍyÄÚÈÝÒ»Ö±²»±ä£¬²»¹ÜÄãÖظ´x.equals(y)¶àÉٴΣ¬·µ»Ø¶¼ÊÇ“true”¡£
• ÈκÎÇé¿öÏ£¬x.equals(null)£¬ÓÀÔ¶·µ»ØÊÇ“false”£»x.equals(ºÍx²»Í¬ÀàÐ͵ĶÔÏó)ÓÀÔ¶·µ»ØÊÇ“false”¡£
ÒÔÉÏÕâÎåµãÊÇÖØ
Ïà¹ØÎĵµ£º
LinkedListÄÚ²¿ÒÔÁ´±íÐÎʽ´æ´¢Êý¾Ý
ArrayListÄÚ²¿ÒÔÊý×éÐÎʽ´æ´¢Êý¾Ý¡£
VectorͬArrayList£¬²»¹ýËüÓëArrayList±È½ÏÆðÀ´ÊÇthread-safeµÄ¡£
HashtableÊǼ̳ÐÁËDictionary£¬ÊÇḬ̈߳²È«µÄ¡£HashMapʵÏÖÁËMap½Ó¿Ú£¬²»ÊÇḬ̈߳²È«µÄ¡£
ÈçºÎ±£Ö¤Ḭ̈߳²È«µÄ£¿Ã¿¸öÐÞ¸ÄÈÝÆ ......
package Utils.Sort;
/**
*¿ìËÙÅÅÐò£¬ÒªÇó´ýÅÅÐòµÄÊý×é±ØÐëʵÏÖComparable½Ó¿Ú
*/
public class QuickSort implements SortStrategy
{
private static final int CUTOFF = 3; //µ±ÔªËØÊý´óÓÚ´Ë ......
´ò¿ªÖնˣ¬Ö´ÐÐÒÔÏÂÃüÁ»òʹÓÃAdept/ÐÂÁ¢µÃÈí¼þ¹ÜÀíÆ÷£¬ÔÚÆäÖзֱðËÑË÷"sun-java6-jre"ºÍ"sun-java6-jdk"²¢±ê¼Ç°²×°¡£
sudo apt-get install sun-java6-jre
Èç¹û¿Õ¼ä¸»Ô££¬½¨Òé°²×°Ò»¸öJDK¡£
sudo apt-get install sun-java6-jdk
Ìáʾ£º°²×°¹ý³ÌÖÐÐèÒªÄã»Ø´ðÊÇ·ñͬÒâʹÓÃÐÒ飨ÖÕ¶ËÖкìÀ¶É«µÄÌ ......
Java·Ö³ÉJ2ME£¨Òƶ¯Ó¦Óÿª·¢£©£¬J2SE£¨×ÀÃæÓ¦Óÿª·¢£©£¬J2EE(WebÆóÒµ¼¶Ó¦ÓÃ)£¬ËùÒÔJava²¢²»Êǵ¥»ú°æµÄ£¬Ö»ÊÇÃæÏò¶ÔÏóÓïÑÔ¡£½¨ÒéÈç¹ûѧϰJavaÌåϵµÄ»°¿ÉÒÔÕâÑùȥѧϰ£º
*µÚÒ»½×¶Î£ºJava»ù´¡£¬°üÀ¨JavaÓï·¨£¬ÃæÏò¶ÔÏóÌØÕ÷£¬³£¼ûAPI£¬¼¯ºÏ¿ò¼Ü£»
*µÚ¶þ½×¶Î£ºJava½çÃæ±à³Ì£¬°üÀ¨AWT£¬Ê¼þ»úÖÆ£¬SWING£¬Õâ¸ö²¿ ......
JavaÓïÑÔ»ù´¡
̸µ½JavaÓïÑÔ»ù´¡Ñ§Ï°µÄÊé¼®£¬´ó¼Ò¿Ï¶¨»áÍƼöBruce EckelµÄ¡¶ThinkinginJava¡·¡£ËüÊÇÒ»±¾Ð´µÄÏ൱Éî¿ÌµÄ¼¼ÊõÊé¼®£¬JavaÓïÑÔ»ù´¡²¿·Ö»ù±¾Ã»ÓÐÆäËüÈκÎÒ»±¾Êé¿ÉÒÔ³¬Ô½Ëü¡£¸ÃÊéµÄ×÷ÕßBruceEckelÔÚÍøÂçÉϱ»³ÆΪÌì²ÅµÄͶ»úÕߣ¬×÷Õߵġ¶Thinking in C++¡·ÔÚ1995ÄêÔø»ñSoftwareDevelopmentJoltAward×î¼ÑÊé¼®´ó½±£¬¡ ......