¼òµ¥»Ø¹ËJava»ù±¾¸ÅÄî
ÔÝʱÕâÑùÀí½â£¬Ï£Íû´ó¼ÒÖ¸³öÎҵĴíÎó
1.hashcode and equals:
equalsÈç¹ûÓÃÀ´ÅжÏÁ½¸ö¶ÔÏóµÄÄÚÈÝÊÇ·ñÏàͬ£¬ÐèÒªÖØд£¬ÒòΪequals() ĬÈÏÊDZȽÏÁ½¸ö¶ÔÏóµÄÒýÓá£
hashCode() ÊÇÓÃÀ´²úÉú¹þÏ£ÂêµÄ£¬¶ø¹þÏ£ÂêÊÇÓÃÀ´ÔÚÉ¢Áд洢½á¹¹ÖÐÈ·¶¨¶ÔÏóµÄ´æ´¢µØÖ·µÄ£¬Ïóutil°üÖÐµÄ ´ø hash µÄ¼¯ºÏÀ඼ÊÇÓÃÕâÖÖ´æ´¢½á¹¹ £ºHashMap,HashSet, ËûÃÇÔÚ½«¶ÔÏó´æ´¢Ê±£¨Ñϸñ˵ÊǶÔÏóÒýÓã©£¬ÐèҪȷ¶¨ËûÃǵĵØÖ·°É£¬¶øHashCode()¾ÍÊÇÕâ¸öÓÃ;µÄ£¬Ò»°ã¶¼ÐèÒªÖØж¨ÒåËüµÄ£¬ÒòΪĬÈÏÇé¿öÏ£¬ÓÉ Object ÀඨÒåµÄ hashCode ·½·¨»áÕë¶Ô²»Í¬µÄ¶ÔÏ󷵻ز»Í¬µÄÕûÊý£¬ÕâÒ»°ãÊÇͨ¹ý½«¸Ã¶ÔÏóµÄÄÚ²¿µØַת»»³ÉÒ»¸öÕûÊýÀ´ÊµÏֵģ¬ÏÖÔÚ¾Ù¸öÀý×ÓÀ´Ëµ£¬ ¾ÍÄÃHashSetÀ´Ëµ£¬ÔÚ½«¶ÔÏó´æÈëÆäÖÐʱ£¬Í¨¹ý±»´æÈë¶ÔÏóµÄ hashCode() À´È·¶¨¶ÔÏóÔÚ HashSet ÖеĴ洢µØÖ·£¬Í¨¹ýequals()À´È·¶¨´æÈëµÄ¶ÔÏóÊÇ·ñÖظ´£¬hashCode() £¬equals()¶¼ÐèÒª×Ô¼ºÖØж¨Ò壬ÒòΪhashCode()ĬÈÏÇ°ÃæÒѾ˵À²£¬¶øequals() ĬÈÏÊDZȽϵĶÔÏóÒýÓã¬ÄãÏÖÔÚÏëһϣ¬Èç¹ûÄã²»¶¨Òåequals()µÄ»°£¬ÄÇôͬһ¸öÀà²úÉúµÄÁ½¸öÄÚÈÝÍêÈ«ÏàͬµÄ¶ÔÏ󶼿ÉÒÔ´æÈëSet£¬ÒòΪËûÃÇÊÇͨ¹ý equals()À´È·¶¨µÄ£¬ÕâÑù¾ÍʹµÃHashSet ʧȥÁËËûµÄÒâÒå¡£
¿´Ò»ÏÂÏÂÃæÕâ¸ö£º
public class Test {
public static void main(String[] args) {
HashSet set = new HashSet();
for (int i = 0; i <= 3; i++){
set.add(new Demo1(i,i));
}
System.out.println(set);
set.add(new Demo1(1,1));
System.out.println(set);
System.out.println(set.contains(new Demo1(0,0)));
System.out.println(set.add(new Demo1(1,1)));
System.out.println(set.add(new Demo1(4,4)));
System.out.println(set);
}
private static class Demo1 {
private int value;
private int id;
public Demo1(int value,int id) {
this.value = value;
this.id=id;
}
public String toString() {
return " value = " + value;
}
public boolean equals(Object o) {
Demo1 a = (Demo1) o;
return (a.value == value) ? true : false;
}
public int hashCode() {
return id;
}
}
}
½á¹û£º
[ value = 2, value = 1, value = 3, value = 0]
[ value = 2, value = 1, value = 3, value = 0]
true
false
true
[ value = 2, value = 4, value = 1, value = 3, value =
Ïà¹ØÎĵµ£º
JavaΪÊý¾Ý½á¹¹µÄÓ³É䶨ÒåÁËÒ»¸ö½Ó¿Újava.util.Map.ËüÓÐËĸöʵÏÖÀ࣬·Ö±ðÊÇHashMap, Hashtable,LinkedHashMapºÍTreeMapÖ÷ÒªÓÃÓÚ´æ´¢¼üÖµ¶Ô£¬¸ù¾Ý¼üµÃµ½Öµ£¬Òò´Ë²»ÔÊÐí¼üÖظ´£¬µ«ÔÊÐíÖµÖظ´¡£
HashMapÊÇÒ»¸ö×î³£ÓõÄMap£¬Ëü¸ù¾Ý¼üµÄHashCodeÖµ´æ´¢Êý¾Ý£¬¸ù¾Ý¼ü¿ÉÒÔÖ±½Ó»ñÈ¡ËüµÄÖµ£¬¾ßÓкܿìµÄ·ÃÎÊËٶȣ¬± ......
8ÖÖ»ù±¾Êý¾ÝÀàÐÍÓÐÏàÓ¦µÄ8ÖÖ°ü×°Àà(Wrapper)¶ÔÓ¦,·Ö±ðÊÇ:Character,Byte,Short,Integer,Long,Float,Double,Boolean.
Ìصã:
1.ÌṩÁËvalueOf(String)ÓÃÓÚ´Ó×Ö·û´®×ª»»¼°toString()ת»»³É×Ö·û´®
2.ͨ¹ýxxxxValue()·½·¨¿ÉÒԵõ½Ëù°ü×°µÄÖµ,ÈçInteger¶ÔÏóµÄintValue()·½·¨.
ʾÀý´úÂë:
public class DoubleAndString {
pu ......
ÄÚ´æÒç³öÓëÊý¾Ý¿âËø±íµÄÎÊÌ⣬¿ÉÒÔ˵ÊÇ¿ª·¢ÈËÔ±µÄجÃΣ¬Ò»°ãµÄ³ÌÐòÒì³££¬×ÜÊÇ¿ÉÒÔÖªµÀÔÚʲôʱºò»òÊÇÔÚʲô²Ù×÷²½ÖèÉϳöÏÖÁËÒì³££¬¶øÇÒ¸ù¾Ý¶ÑÕ»ÐÅÏ¢Ò²ºÜÈÝÒ׶¨Î»µ½³ÌÐòÖÐÊÇij´¦³öÏÖÁËÎÊÌâ¡£ÄÚ´æÒç³öÓëËø±íÔò²»È»£¬Ò»°ãÏÖÏóÊDzÙ×÷Ò»°ãʱ¼äºóϵͳԽÀ´Ô½Âý£¬Ö±µ½ËÀ»ú£¬µ«²¢²»ÄÜÃ÷È·ÊÇÔÚʲô²Ù×÷ÉϳöÏֵģ¬·¢ÉúµÄʱ¼äµãҲûÓÐ ......
Òì³£ÁÐ±í£º
1. java.lang.nullpointerexception
2. Java.lang.classnotfoundexception
3. Java.lang.arithmeticexception
4. Java.lang.arrayindexoutofboundsexception
5. Java.lang.illegalargumentexception
6. java.lang.illegalaccessexception
1. java.lang.nullpoi ......
ÏßÐÔ±í£¬Á´±í£¬¼¯ºÏ£¬¹þÏ£±íÊdz£ÓõÄÊý¾Ý½á¹¹£¬ÔÚ½øÐÐJava¿ª·¢Ê±£¬JDKÒѾΪÎÒÃÇÌṩÁËһϵÁÐÏàÓ¦µÄÀàÀ´ÊµÏÖ»ù±¾µÄÊý¾Ý½á¹¹¡£ÕâЩÀà¾ùÔÚjava.util°üÖС£
Collection
©ÀList
©¦©ÀLinkedList
©¦©ÀArrayList
©¦©¸Vector
©¦¡¡©¸Stack
©¸Set
Map
©ÀHashtable
©ÀHashMap
©¸WeakHashMap
Ö÷Òª·ÖΪÁ½¸ö·ÖÖ§£º
Collectio ......