Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UUID to unique integer id?

Tags:

java

uuid

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?

like image 539
Alex Hope O'Connor Avatar asked Apr 06 '11 08:04

Alex Hope O'Connor


People also ask

Can UUID be integer?

No, the hash code is not (and cannot be) unique.

Is UUID a number or string?

A UUID is made up of hex digits (4 chars each) along with 4 “-” symbols, which make its length equal to 36 characters. The Nil UUID is a special form of UUID in which all bits are set to zero. In this tutorial, we will have a look at the UUID class in Java.

Is Java UUID really unique?

A UUID is 36 characters long unique number. It is also known as a Globally Unique Identifier (GUID). A UUID is a class that represents an immutable Universally Unique Identifier (UUID). A UUID represents a 128-bit long value that is unique to all practical purpose.

What is a UUID (GUID)?

UUID stands for Universally Unique Identifier (sometimes called "GUID" or "Globally Unique Identifiers"). UUIDs are 36 character strings containing numbers, letters and dashes. UUIDs are designed to be globally unique. There are several UUID versions with slightly different purposes.

What is a unique identifier (UID)?

A Unique Identifier is a unique identifier for any kind of object. This goes for things like Patient Identifiers, DICOM Study Instance (DICOM calls these a UID), and CDA Documents. To a computer a communications protocol, these are just a Unique Identifier of an Object. Like homeCommunityId, RepositoryUniqueId, etc.

Is it possible to store a UUID in an integer?

Yes: if you have a 128-bit integer, you can store it in an integer. You could also use two 64-bit integers or four 32-bit integers. You can also store them as a string. @paddy, The UUID I'm talking about is something like this: 002D0BF8-8AA3-120D-A933-6DDE51F15329.

Is there a way to prove that a UUID is unique?

These are not absolutely unique, but statistically unique. Not that there is ever a way to prove that any Unique ID is absolutely unique, but UUID is a fixed size and without a structure that tries to guarantee uniqueness.


2 Answers

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)

like image 185
Jeff Foster Avatar answered Sep 27 '22 01:09

Jeff Foster


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")

like image 25
pgras Avatar answered Sep 25 '22 01:09

pgras