UUID to unique integer id?

JavaUuid

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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)

Solution 2 - Java

Answering the How can I have a unique application wide Integer:

If it needs to be unique even after restarts or if you application is clustered you may use a Database sequence.

If it just needs to be unique during the runtime use a static AtomicInteger.

EDIT (example added):

public class Sequence {

  private static final AtomicInteger counter = new AtomicInteger();

  public static int nextValue() {
    return counter.getAndIncrement();
  }
}

Usage:

int nextValue = Sequence.nextValue();

This is thread safe (different threads will always receive distinct values, and no values will be "lost")

Solution 3 - Java

A UUID is a 16-Byte number (128 bit). You can't crunch it into an int (32 bit) while preserving it's uniqueness.

Mathematically spoken: 296 UUIDs will share the same Java-int-size hash value (which is ... a lot ;) )

A way out - some real life UUID often have a rather static part. So in isolated scenarios, the real unique portion of UUIDs may be less then 32 bit.

Solution 4 - Java

We had a requirement to convert all our UUIDs into serial numbers. Finally, we tested and used the next algorithm:

  1. Get CRC64 of uuid(16 bytes) using the ECMA polynomial 0xC96C5795D7870F42. Do not use ISO polynomial because it could cause a lot of collisions for some UUID generation algorithms.

  2. Now we have crc64(8 bytes). Take the first N bytes(in our case 5 in yours it will be 4 bytes for int and all bytes for int64)

We tested this method and it works well for several million UUIDs.

Our additional step: convert 5 bytes number into a number with base 36 and finally we have SN: 4YD3SOJB.

Solution 5 - Java

No, the hash code is not (and cannot be) unique. The thing with a GUID/UUID is that you need all 128 bits to guarantee uniqueness, therefore scaling it down in any way will give problems, see e.g. GUIDs are globally unique, but substrings of GUIDs aren't.

Honestly, I think you're better off with just using sequential integers and skip the GUID thing altogether. If you need GUIDs for any reason, then use them and don't try generating an integer from them.

Solution 6 - Java

You may convert uuid to BigInteger and keep it unique.

              BigInteger  big = new BigInteger(uuid, 16);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex Hope O'ConnorView Question on Stackoverflow
Solution 1 - JavaJeff FosterView Answer on Stackoverflow
Solution 2 - JavapgrasView Answer on Stackoverflow
Solution 3 - JavaAndreas DolkView Answer on Stackoverflow
Solution 4 - JavaHodzaView Answer on Stackoverflow
Solution 5 - JavaJoeyView Answer on Stackoverflow
Solution 6 - JavayangjunView Answer on Stackoverflow