如果在Java中添加了另外两个哈希码生成一个新的哈希码,那么发生碰撞的概率是多少?
例如:
Integer reportHashCode = reportFields.hashCode() + reportId.hashCode();
假设Java的哈希码是32位,我们可以忽略哈希码本身的普通冲突。
我建议在这里使用XOR运算符而不是加法,因为XOR具有50-50%的1和0分布。
package hashcode;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
public class HashCode {
// Number of test cases
private static final int TEST_CASES = 10_000_000;
public static void main(String[] args) {
// Random number generator
Random rand = new Random();
// Map from integers (result hash codes) to a list of addend pairs that formed those hash codes
Map<Integer, Set<Pair>> hashCodesToComposites = new HashMap<>();
// Number of collissions
int collisions = 0;
// Running simulations
for (int i = 0; i < TEST_CASES; ++i) {
if (TEST_CASES / 4 == i) {
System.out.println("25 %");
}
if (TEST_CASES / 2 == i) {
System.out.println("50 %");
}
if ((TEST_CASES * 3) / 4 == i) {
System.out.println("75 %");
}
// Generating addends as random integers
int first = rand.nextInt();
int second = rand.nextInt();
// The pair; its hash code is the sum of the above
Pair pair = new Pair(first, second);
// Did it occur before?
if (hashCodesToComposites.containsKey(pair.hashCode())) {
// Getting the set of addend pairs that created this hash code
Set<Pair> composites = hashCodesToComposites.get(pair.hashCode());
// Checking if by any chance the two random numbers happened to be the same (almost negligible)
if (!composites.contains(pair)) {
// Actual collision from different numbers
collisions++;
// Adding to the set of composites
composites.add(pair);
} // Same numbers; doesn't count as collision
} else {
// First occurrence of this hash code
Set<Pair> composites = new HashSet<>();
composites.add(pair);
hashCodesToComposites.put(pair.hashCode(), composites);
}
}
// Results
System.out.println("Test cases: " + TEST_CASES);
System.out.println("Collisions: " + collisions);
System.out.println("Probability: " + ((double) collisions / (double) TEST_CASES));
}
private static class Pair {
final int first;
final int second;
final int hashCode;
Pair(int first, int second) {
this.first = first;
this.second = second;
hashCode = first + second;
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
final Pair other = (Pair) obj;
return (this.first == other.first && this.second == other.second) || (this.first == other.second && this.second == other.first);
}
}
}
package hashcode;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class HashCode2 {
// Number of test cases
private static final int TEST_CASES = 10_000_000;
public static void main(String[] args) {
// Random number generator
Random rand = new Random();
Set<Integer> hashCodes = new HashSet<>();
// Number of collissions
int collisions = 0;
// Running simulations
for (int i = 0; i < TEST_CASES; ++i) {
if (TEST_CASES / 4 == i) {
System.out.println("25 %");
}
if (TEST_CASES / 2 == i) {
System.out.println("50 %");
}
if ((TEST_CASES * 3) / 4 == i) {
System.out.println("75 %");
}
int next = rand.nextInt();
if (hashCodes.contains(next)) {
collisions++;
} else {
hashCodes.add(next);
}
}
// Results
System.out.println("Test cases: " + TEST_CASES);
System.out.println("Collisions: " + collisions);
System.out.println("Probability: " + ((double) collisions / (double) TEST_CASES));
}
}
Pair
的hashCode方法中使用了异或运算而不是加法。结果呢?基本上又是一样的。因此,最终你可以期望得到非常接近于随机整数的碰撞率,对于两个哈希码的和与异或运算,前提是这两个哈希码被相加/异或的分布很好。
xor
的另一个原因。但即使发生溢出,哈希映射/集合中使用的哈希码也不会受到影响。int的溢出仍然是int。 - Eugene