如果我理解正确,在.NET中,默认实现Object.GetHashCode()返回一个基于对象的内存地址的值(至少对于引用类型).但是,垃圾收集器可以在内存中自由移动对象.据推测,哈希代码不会因为GC移动对象而改变,所以这种交互是否有特殊处理,或者我的假设是错误的?
是否有任何理由不能使用hashCode方法测试Java字符串的相等性?所以基本上,而不是....
"hello".equals("hello")
Run Code Online (Sandbox Code Playgroud)
你可以用......
"hello".hashCode() == "hello".hashCode()
Run Code Online (Sandbox Code Playgroud)
这很有用,因为一旦字符串计算了它的哈希码,那么比较一个字符串就像比较一个int一样有效,因为字符串缓存了哈希码,而且很可能字符串在字符串池中,如果你设计它办法.
我想知道GetHashCode()在string实例上调用方法时获取重复值的可能性.例如,根据这篇博文, blair并brainlessness在x86机器上具有相同的哈希码(1758039503).
这是md5哈希的3个示例
$ md5 -s "1" && md5 -s "2" && md5 -s "3"
MD5 ("1") = c4ca4238a0b923820dcc509a6f75849b
MD5 ("2") = c81e728d9d4c2f636f067f89cc14862c
MD5 ("3") = eccbc87e4b5ce2fe28308fd9f2a7baf3
Run Code Online (Sandbox Code Playgroud)
说我想从任何哈希中取8个字符.哈希的开头部分是否比结尾更"随机"?中间?或者所有子串都是"随机"的?
有没有办法在postgresql中获取行的哈希码?
我需要导出一些数据,只有在最后一次导出后数据有一些变化,最后导出的数据行可以存储在一个表中,当我再次需要导出数据时我可以得到所有的哈希值数据并仅导出具有与上次导出不同的哈希值的行.
是否可以使用postgresql实现?
谢谢
有以下课程:
public class Member {
private int x;
private long y;
private double d;
public Member(int x, long y, double d) {
this.x = x;
this.y = y;
this.d = d;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = (int) (prime * result + y);
result = (int) (prime * result + Double.doubleToLongBits(d));
return result;
}
@Override
public boolean equals(Object obj) {
if (this …Run Code Online (Sandbox Code Playgroud) 以下java代码返回字符串的哈希码.
String uri = "Some URI"
public int hashCode() {
return uri.hashCode();
}
Run Code Online (Sandbox Code Playgroud)
我想将此代码翻译为c ++.c ++中是否有任何可用的功能或一种简单的方法来翻译它.
Object在Java中有hashCode方法,但它只在像HashSet或的关联容器中使用HashMap.它为什么这样设计?Hashable具有hashCode方法的界面看起来更优雅的解决方案.
如果在HotSpot Java 7 64位版本上运行以下命令.
int countTopBit = 0, countLowestBit = 0;
for (int i = 0; i < 100000000; i++) {
int h = new Object().hashCode();
if (h < 0)
countTopBit++;
if ((h & 1) == 1)
countLowestBit++;
}
System.out.println("The count of negative hashCodes was " + countTopBit + ", the count of odd hashCodes was " + countLowestBit);
Run Code Online (Sandbox Code Playgroud)
你可以得到像这样的结果
The count of negative hashCodes was 0, the count of odd hashCodes was 49994232
Run Code Online (Sandbox Code Playgroud)
我想知道这是否意味着Object.hashCode()它只是真正的31位,为什么会这样呢?
不是不使用顶部位的情况.来自HashMap的源代码
257 /** …Run Code Online (Sandbox Code Playgroud) 我有一个包含不同枚举(不同类型)的类.该类用作a的键HashMap.hashCode类目前实现如下:
public static class Key implements Comparable<Key> {
final int a;
final Enum1 enum1;
final Enum2 enum2;
@Override
public int hashCode() {
return a ^ enum1.hashCode() ^ enum2.hashCode();
}
// ... definition of equals and toString ...
}
Run Code Online (Sandbox Code Playgroud)
现在,如果枚举hashCode只返回枚举定义中枚举值的索引,这将不是最佳的(太多冲突).方法定义Enum.hashCode()是这样的:
/**
* Returns a hash code for this enum constant.
*
* @return a hash code for this enum constant.
*/
public final int hashCode() {
return super.hashCode();
}
Run Code Online (Sandbox Code Playgroud)
假设这个委托Object.hashCode(),一切都应该没问题,因为每个枚举常量只存在一个实例,Object.hashCode()理论上就是从对象的内部地址派生的整数.我对吗?
PS:当在键中多次使用相同的枚举时,你将不得不使用更复杂的东西.