Long computing concept situation hash value and hash collision examples

I. Background

Author of "Ali Baba Java Development Manual" lone make first class chiefs of DIY organized, from the initial hundreds of people, there are only 60 people adhere to the present.

First, Deeply Inspire Yourself stimulate their depth

Second, Do It Yourself Practice makes perfect

Of 20 wherein the subject is: choose a category, its hashCode method described design and code the core logic.

I choose Long class analysis.

Second, analysis

2.1 choose a category, its hashCode method described design and code the core logic.

java.lang.Long#hashCode(long)

/**

* Returns a hash code for this {@code Long}. The result is

* the exclusive OR of the two halves of the primitive

* {@code long} value held by this {@code Long}

* object. That is, the hashcode is the value of the expression:

*

* <blockquote>

* {@code (int)(this.longValue()^(this.longValue()>>>32))}

* </blockquote>

*

* @return a hash code value for this object.

*/

@Override

public int hashCode() {

return Long.hashCode(value);

}

Design philosophy :

Long int 34 is 64 bits and the hash value is the int type, so the 64-bit into 32-bit or less.

Let the high and low are able to participate in the hash calculation, so the result of the discrete.

 

Core logic : long hash value is obtained by the first half and the second half of the exclusive OR obtained.

public static int hashCode(long value) {

return (int)(value ^ (value >>> 32));

}

 

 

2.2 How to construct hash conflict

 

Since the hash value is to long to obtain 32-bit 64-bit arithmetic, i.e. speak to a wide range of small-scale map, so there must be a hash conflict.

The Long hash function mapping rule, a simple construction can be seen best way hash is based on the rules, the low and high consistency 0 and 1 interchanged or interchanged, the operation result.

 

// [1] high and low exchange

long value = Long.MAX_VALUE - 1024L;

System.out.println(Long.toBinaryString(value));

// 取高32位

long high32 = value >>> 32;

// 取高32

long low32 = value << 32;

// 高 32 和 低32 互换

long newValue = high32 | low32;

// 新的hash 值 和原值相同

System.out.println(newValue);

Assert.assertEquals(Long.hashCode(value), Long.hashCode(newValue));

[2] // 0 and 1 swap

// 高位取反,低位 0 填充

newValue = Long.MAX_VALUE & value;

System.out.println(newValue);

Assert.assertEquals(Long.hashCode(value), Long.hashCode(newValue));

Value can be quickly determined by the two hash values ​​are different, then the same function is determined by equals.

java.lang.Long#equals

public boolean equals(Object obj) {

  if (obj instanceof Long) {
    return value == ((Long)obj).longValue();
   }

  return false;
}

 

But also to modify two objects hashcode function ensures equal hashcode rewriting equals the same constant.

The only way to ensure equal then call equals determine whether the case is likely to determine the priority by hashcode phase (the same hash value) and the like.

 

The nature of the hash function is encoded into digital data, and the java hashcode return type of int, if the hash function badly written, or some number greater than that of an integer type, a conflict may arise inevitably (Ha different objects of the same Greek values).

 

Generally so that every moderate means are involved in the operation designed to better hash function, hash avoid conflict as much as possible.

After the conflict usually need to be addressed by re-hash, zippers, etc. to public overflow.

 

Similarly, you can also select other classes, such as String, Integer, etc., to see their calculated hash values, study how to construct a hash conflict.

Third, the summary

Many people see the source code developed in less time, leading to a lot of points smattering of knowledge.

Some problems do solitary Gangster by DIY classes, so I also have to develop a habit of reading the source code.

I hope we can take the issue to learn, usually occasionally to look at the source code in the development.

 

 

 

Published 379 original articles · won praise 862 · Views 1.32 million +

Guess you like

Origin blog.csdn.net/w605283073/article/details/103001627