UUID to unique integer id?

Alex Hope O'Connor picture Alex Hope O'Connor · Apr 6, 2011 · Viewed 90.1k times · Source

I was wondering what the easiest way to convert a UUID to a unique integer would be? I have tried using the hash code but people tell me that it is not going to always be unique if i use the hash code?

So what is the easiest way? Is the hash code unique?

Answer

Jeff Foster picture Jeff Foster · Apr 6, 2011

You are going to have a problem since the UUID is 128 bits and the int is only 32bit. You'll either have to accept the risk of collisions and try to fudge it to a smaller space (hashCode is probably a good way to do that) or find an alternative (use the UUID directly, map to a BigInteger - difficult to tell without knowing why)