1. 程式人生 > 其它 >兩個字串的hashCode相同但這兩個字串不相等的情況

兩個字串的hashCode相同但這兩個字串不相等的情況

由String中hashcode的原始碼:

public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;
 
            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        
return h; }

可知

str.charAt(0) * 31^(n-1) + str.charAt(1) * 31^(n-2) + ... + str.charAt(n-1)

那我們最簡單的操作就是當n=2的時候

x1*31 + x2 = y1*31 + y2

可得(x1-y1) * 31 = y2 - x2;

大家可以去查查Ascii表中的Ascii碼 找出x1,x2與y1,y2滿足上面式子的情況

 

我這邊找到的是"3C"與"2b"

轉載:https://blog.csdn.net/Cia_zibo/article/details/104870197

 

 

TRANSLATE with
x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
  TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back