Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>since you can read chinese you can look at my post <a href="http://www.hetaoblog.com/myblogs/post/%E8%AF%B4%E4%B8%80%E8%AF%B4java%E9%87%8C%E9%9D%A2%E7%9A%84hashcode-string-hashcode.jhtml">http://www.hetaoblog.com/myblogs/post/%E8%AF%B4%E4%B8%80%E8%AF%B4java%E9%87%8C%E9%9D%A2%E7%9A%84hashcode-string-hashcode.jhtml</a></p> <p>see a test method, basically, so long as you match, a1*31+b1 = a2*31 +b2, which means (a1-a2)*31=b2-b1</p> <pre><code>public void testHash() { System.out.println("A:" + ((int)'A')); System.out.println("B:" + ((int)'B')); System.out.println("a:" + ((int)'a')); System.out.println(hash("Aa".hashCode())); System.out.println(hash("BB".hashCode())); System.out.println(hash("Aa".hashCode())); System.out.println(hash("BB".hashCode())); System.out.println(hash("AaAa".hashCode())); System.out.println(hash("BBBB".hashCode())); System.out.println(hash("AaBB".hashCode())); System.out.println(hash("BBAa".hashCode())); } </code></pre> <p>you will get </p> <pre><code>A:65 B:66 a:97 2260 2260 2260 2260 2019172 2019172 2019172 2019172 </code></pre> <p><strong>edit: someone said this is not straightforward enough. I added below part</strong></p> <pre><code> @Test public void testN() throws Exception { List&lt;String&gt; l = HashCUtil.generateN(3); for(int i = 0; i &lt; l.size(); ++i){ System.out.println(l.get(i) + "---" + l.get(i).hashCode()); } } AaAaAa---1952508096 AaAaBB---1952508096 AaBBAa---1952508096 AaBBBB---1952508096 BBAaAa---1952508096 BBAaBB---1952508096 BBBBAa---1952508096 BBBBBB---1952508096 </code></pre> <p>below is the source code, it might be not efficient, but it work:</p> <pre><code>public class HashCUtil { private static String[] base = new String[] {"Aa", "BB"}; public static List&lt;String&gt; generateN(int n) { if(n &lt;= 0) { return null; } List&lt;String&gt; list = generateOne(null); for(int i = 1; i &lt; n; ++i) { list = generateOne(list); } return list; } public static List&lt;String&gt; generateOne(List&lt;String&gt; strList) { if((null == strList) || (0 == strList.size())) { strList = new ArrayList&lt;String&gt;(); for(int i = 0; i &lt; base.length; ++i) { strList.add(base[i]); } return strList; } List&lt;String&gt; result = new ArrayList&lt;String&gt;(); for(int i = 0; i &lt; base.length; ++i) { for(String str: strList) { result.add(base[i] + str); } } return result; } } </code></pre> <p>look at String.hashCode()</p> <pre><code> public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i &lt; len; i++) { h = 31*h + val[off++]; } hash = h; } return h; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload