Last active
January 12, 2016 08:52
-
-
Save gilinachum/df7031cf349fde91f3f1 to your computer and use it in GitHub Desktop.
Empirically tests degree of uniqueness when converting UUIDs to hashcode values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package collisions.test; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.UUID; | |
public class UUIDToHashcodeUniquenessTestMain { | |
private final static int num_of_users = 1000 * 1000; | |
private final static int num_of_stacks = 50; | |
public static void main(String[] args) { | |
int collisions = 0; | |
for (int i = 0; i < num_of_users; i++) { | |
collisions += calcCollisionsForUser(); | |
} | |
System.out.println("Had " + collisions + " collisions for " + num_of_users + " users"); | |
} | |
private static int calcCollisionsForUser() { | |
int collisions = 0; | |
Set<Integer> uuidSet = new HashSet<Integer>(num_of_stacks * 2); | |
for (int i = 0; i < num_of_stacks; i++) { | |
String uuid = UUID.randomUUID().toString(); | |
Integer uuidHashcode = uuid.hashCode(); | |
if (uuidSet.contains(uuidHashcode)) { | |
collisions++; | |
} | |
uuidSet.add(uuidHashcode); | |
} | |
return collisions; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment