java有没有不会冲突的hash算法

Python012

java有没有不会冲突的hash算法,第1张

java.util.HashMap的中put方法的具体实现:

static inline intptr_t get_next_hash(Thread * Self, oop obj) {

intptr_t value = 0

if (hashCode == 0) {

// This form uses an unguarded global Park-Miller RNG,

// so it's possible for two threads to race and generate the same RNG.

// On MP system we'll have lots of RW access to a global, so the

// mechanism induces lots of coherency traffic.

value = os::random()

} else

if (hashCode == 1) {

// This variation has the property of being stable (idempotent)

// between STW operations. This can be useful in some of the 1-0

// synchronization schemes.

intptr_t addrBits = intptr_t(obj) >>3

value = addrBits ^ (addrBits >>5) ^ GVars.stwRandom

} else

if (hashCode == 2) {

value = 1// for sensitivity testing

} else

if (hashCode == 3) {

value = ++GVars.hcSequence

} else

if (hashCode == 4) {

value = intptr_t(obj)

} else {

// Marsaglia's xor-shift scheme with thread-specific state

// This is probably the best overall implementation -- we'll

// likely make this the default in future releases.

unsigned t = Self->_hashStateX

t ^= (t <<11)

Self->_hashStateX = Self->_hashStateY

Self->_hashStateY = Self->_hashStateZ

Self->_hashStateZ = Self->_hashStateW

unsigned v = Self->_hashStateW

v = (v ^ (v >>19)) ^ (t ^ (t >>8))

Self->_hashStateW = v

value = v

}

value &= markOopDesc::hash_mask

if (value == 0) value = 0xBAD

assert (value != markOopDesc::no_hash, "invariant")

TEVENT (hashCode: GENERATE)

return value

}

该实现位于hotspot/src/share/vm/runtime/synchronizer.cpp文件下。

java使用哈希值判断通过hash算法比对对象是否修改。根据查询相关公开信息显示,使用string.GetHashCode()方法,将用户对象序列化成字符串,用string.GetHashCode()方法,获取字符串的哈希值,当用户点击保存按钮保存数据时即可判断对象是否修改。