java - Why does the method hash(int h) of HashMap like this? -


this question has answer here:

static int hash(int h) {     // function ensures hashcodes differ     // constant multiples @ each bit position have bounded     // number of collisions (approximately 8 @ default load factor).     h ^= (h >>> 20) ^ (h >>> 12);     return h ^ (h >>> 7) ^ (h >>> 4); } 

could tell me why hash method designned this?what benefit?

if see open jdk source,

this method have comments...

/**           * applies supplemental hash function given hashcode,           * defends against poor quality hash functions.  critical           * because hashmap uses power-of-two length hash tables,           * otherwise encounter collisions hashcodes not differ           * in lower bits. note: null keys map hash 0, index 0.  */ 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -