Sta*_*kER 11 java string hashcode
用Java编写的现有系统使用字符串的哈希码作为其负载平衡的路由策略.
现在,我无法修改系统,但需要生成共享相同哈希码的字符串以测试最差条件.
我从命令行提供这些字符串,并希望系统将所有这些字符串路由到同一目的地.
是否可以生成共享相同哈希码的大量字符串?
要明确这个问题:
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
}
Run Code Online (Sandbox Code Playgroud)
备注:任何hashCode值都可以接受.字符串是什么没有约束.但它们应该彼此不同.
编辑:String类的重写方法是不可接受的,因为我从命令行提供这些字符串.
仪表也是不可接受的,因为这会对系统产生一些影响.
het*_*log 24
既然你可以阅读中文,你可以查看我的帖子 http://www.hetaoblog.com/myblogs/post/%E8%AF%B4%E4%B8%80%E8%AF%B4java%E9%87%8C% E9%9D%A2%E7%9A%84hashcode串,hashcode.jhtml
看一个测试方法,基本上,只要你匹配,a1*31 + b1 = a2*31 + b2,这意味着(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()));
}
Run Code Online (Sandbox Code Playgroud)
你会得到
A:65
B:66
a:97
2260
2260
2260
2260
2019172
2019172
2019172
2019172
Run Code Online (Sandbox Code Playgroud)
编辑:有人说这不够直截了当.我在下面添加了部分
@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
Run Code Online (Sandbox Code Playgroud)
下面是源代码,可能效率不高,但它有效:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
看看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;
}
Run Code Online (Sandbox Code Playgroud)
我认为从长字符串中找到一个等于哈希的字符串太难了,当找到一个短字符串(2或3)的等哈希字符串时很容易.看下面的等式.(对不起,我不能发布图片让我成为新成员)
请注意,"FB"和"Ea"具有相同的哈希码,并且任何两个字符串(如s1 +"FB"+ s2和s1 +"Ea"+ s2)将具有相同的哈希码.因此,简单的解决方案是找到现有字符串的任何2-char子字符串,并用具有相同哈希码的2-char子字符串替换
例如,我们有字符串"helloworld" 得到2-char子串"he",hashcode("he")='h'*31 +'e'=('h'*31 + 31)+('e' - 31)=('h'+ 1)*31 +'F'='i'+'F'= hashcode("iF")所以欲望字符串是"iFlloworld"我们将'h'增加1,我们可以增加2或3等(但如果溢出char值则会出错)
下面的代码运行良好的小级别,如果级别很大就会出错,使char值溢出,如果你愿意,我会稍后修改它(这个代码更改2个第一个字符,但是我会将代码编辑为2个最后的字符,因为2个第一个字符是最大值的计算值)
public static String samehash(String s, int level) {
if (s.length() < 2)
return s;
String sub2 = s.substring(0, 2);
char c0 = sub2.charAt(0);
char c1 = sub2.charAt(1);
c0 = (char) (c0 + level);
c1 = (char) (c1 - 31 * level);
String newsub2 = new String(new char[] { c0, c1 });
String re = newsub2 + s.substring(2);
return re;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16245 次 |
最近记录: |