如何在Java中生成共享相同哈希码的字符串? [英] How to generate strings that share the same hashcode in Java?
问题描述
用Java编写的现有系统使用字符串的哈希码作为负载平衡的路由策略。
An existing system written in Java uses the hashcode of a string as its routing strategy for load balancing.
现在,我无法修改系统,但需要生成共享相同哈希码的字符串以测试最差情况。
Now, I cannot modify the system but need to generate strings that share the same hashcode to test the worst condition.
我从命令行提供这些字符串,希望系统将所有这些字符串路由到同一目的地。
I provide those strings from commandline and hope the system will route all these strings into the same destination.
是否可以生成大量共享相同哈希码的字符串?
Is it possible to generate a large numbers of strings that share the same hashcode?
提出这个问题clear:
To make this question clear:
String[] getStringsInSameHashCode(int number){
//return an array in length "number"
//Every element of the array share the same hashcode.
//The element should be different from each other
}
备注:任何hashCode值都是可以接受的。字符串是什么没有约束。但它们应该彼此不同。
Remarks: Any hashCode value is acceptable. There is no constraint on what the string is. But they should be different from each other.
编辑:
不接受字符串类的重写方法,因为我从命令行提供这些字符串。
Override method of String class is not acceptable because I feed those string from command line.
仪器也是不可接受的,因为这会对系统产生一些影响。
Instrumentation is also not acceptable because that will make some impacts on the system.
推荐答案
因为你可以阅读中文,你可以查看我的帖子
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
since you can read chinese you can look at my post 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
查看测试方法,基本上,只要你匹配,
a1 * 31 + b1 = a2 * 31 + b2,表示(a1-a2)* 31 = b2-b1
see a test method, basically, so long as you match, a1*31+b1 = a2*31 +b2, which means (a1-a2)*31=b2-b1
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()));
}
你会得到
A:65
B:66
a:97
2260
2260
2260
2260
2019172
2019172
2019172
2019172
编辑:有人说这不够直截了当。我在下面添加了部分
@Test
public void testN() throws Exception {
List<String> l = HashCUtil.generateN(3);
for(int i = 0; i < 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
下面是源代码,它可能效率不高,但它可以工作:
below is the source code, it might be not efficient, but it work:
public class HashCUtil {
private static String[] base = new String[] {"Aa", "BB"};
public static List<String> generateN(int n)
{
if(n <= 0)
{
return null;
}
List<String> list = generateOne(null);
for(int i = 1; i < n; ++i)
{
list = generateOne(list);
}
return list;
}
public static List<String> generateOne(List<String> strList)
{
if((null == strList) || (0 == strList.size()))
{
strList = new ArrayList<String>();
for(int i = 0; i < base.length; ++i)
{
strList.add(base[i]);
}
return strList;
}
List<String> result = new ArrayList<String>();
for(int i = 0; i < base.length; ++i)
{
for(String str: strList)
{
result.add(base[i] + str);
}
}
return result;
}
}
查看String.hashCode()
look at String.hashCode()
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*h + val[off++];
}
hash = h;
}
return h;
}
这篇关于如何在Java中生成共享相同哈希码的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!