love fish大鵬一曰同風起,扶搖直上九萬里

          導航

          <2006年6月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          公告

          留言簿(15)

          隨筆分類(493)

          隨筆檔案(498)

          相冊

          閱讀排行榜

          常用鏈接

          統(tǒng)計

          積分與排名

          friends

          link

          最新評論

          hashcode重復的可能性超大(轉)

          用String的hashcode得到int類型作為主鍵。其實hashcode重復的可能性超大,下面是java的缺省算法:
           public int hashCode() {
              
          int h = hash;
              
          if (h == 0{
                  
          int off = offset;
                  
          char val[] = value;
                  
          int len = count;

                      
          for (int i = 0; i < len; i++{
                          h 
          = 31*+ val[off++];
                      }

                      hash 
          = h;
                  }

                  
          return h;
              }
          但是什么情況下會重復?下面是測試代碼




          import java.util.HashMap;

          public class Test {

              
          static HashMap map = new HashMap();

              
          private static char startChar = 'A';

              
          private static char endChar = 'z';

              
          private static int offset = endChar - startChar + 1;

              
          private static int dup = 0;

              
          public static void main(String[] args) {
                  
          int len = 3;
                  
          char[] chars = new char[len];
                  tryBit(chars, len);
                  System.
          out.println((int)Math.pow(offset, len) + ":" + dup);
              }


              
          private static void tryBit(char[] chars, int i) {
                  
          for (char j = startChar; j <= endChar; j++{
                      chars[i 
          - 1= j;
                      
          if (i > 1)
                          tryBit(chars, i 
          - 1);
                      
          else
                          test(chars);
                  }

              }


              
          private static void test(char[] chars) {

                  String str 
          = new String(chars).replaceAll("[^a-zA-Z_]""").toUpperCase();// 195112:0
                  
          //String str = new String(chars).toLowerCase();//195112:6612
                  
          //String str = new String(chars).replaceAll("[^a-zA-Z_]","");//195112:122500
                  
          //String str = new String(chars);//195112:138510
                  int hash = str.hashCode();
                  
          if (map.containsKey(hash)) {
                      String s 
          = (String) map.get(hash);
                      
          if (!s.equals(str)) {
                          dup
          ++;
                          System.
          out.println(s + ":" + str);
                      }

                  }
           else {
                      map.put(hash, str);
                      
          // System.out.println(str);
                  }

              }

          }


          在A-z范圍內有特殊字符,從結果看,僅僅3位長度的字符串:
          不處理: 138510次重復
          去掉字母意外字符: 122500次重復
          所有字符轉小寫:6612次重復(少了很多)
          去掉字母意外字符,并且轉小寫:沒有重復!4位字符串也沒見重復

          不難看出:
          1. 缺省實現(xiàn)為英文字母優(yōu)化
          2. 字母大小寫可能導致重復

          可能:
          長字符串可能hashcode重復
          中文字符串和特殊字符可能hashcode重復

          posted on 2006-06-25 10:34 liaojiyong 閱讀(1460) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 万安县| 鸡东县| 东至县| 平和县| 万山特区| 长子县| 林口县| 于都县| 镇原县| 黑水县| 保靖县| 宁陵县| 宝山区| 台南市| 宜昌市| 拜泉县| 阿坝县| 南宁市| 潞城市| 铜鼓县| 江安县| 榆社县| 卢湾区| 南宁市| 稻城县| 固阳县| 潢川县| 隆昌县| 定日县| 焉耆| 宜丰县| 龙里县| 延长县| 蒲城县| 福泉市| 大英县| 平江县| 荥阳市| 营口市| 温泉县| 绥阳县|