如果这个问题真的很奇怪,我很抱歉。我正在将数据发送到我不希望发送重复数据的队列。该程序无法防止重复的数据项,只是接受它们,但我相信队列 ID 必须是唯一的,我可以自己指定这些。所以我想尝试根据我发送的数据生成我的 ID。
假设我有两个字符串:
hello, SO how are you? //base case
hello, SO how are you? //same
hello, SO how are You? //different, notice the capital Y
What up, SO! //completely different
我可以在Java中将它转换成什么来匹配上面的前两个字符串但使另外两个唯一?到目前为止我还没有尝试太多(因为我不确定将其转换为最好的东西是什么),但我确实玩了一下,GZIPOutputStream
但大小似乎有点相似,我不确定它是否是最有效的(即最小的尺寸,比如一些疯狂的十六进制字符或其他东西)。
我希望有人理解我的要求,但如果没有,请告诉我,我会尽力澄清。
编辑:对不起,我不只是想要字符串的东西,我也在处理列表(也许还有字典)。对于那个很抱歉
谢谢
代码:
public class hashtest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
convert("hello world"); //same
convert("hello world"); //same
convert("hello world!"); //exclamation mark
convert("hello World"); //capital W
convert_list(new int[] {5, 2, -3}); //same
convert_list(new int[] {5, 2, -3}); //same
convert_list(new int[] {5, 3, -3}); //different
convert_list(new int[] {5, 2, -4}); //different
}
private static void convert_list(int[] is) {
// TODO Auto-generated method stub
System.out.println("word is " + is);
System.out.println("hashcode is " + is.toString());
System.out.println("****");
}
private static void convert(String string) {
// TODO Auto-generated method stub
System.out.println("word is " + string);
System.out.println("hashcode is " + string.hashCode());
System.out.println("****");
}
}