Create Universally Unique ID in Java

For the longest time I’ve used java.rmi.server.UID to create a unique ID in a application. We enhanced the UID class to fit our needs and to format the ID to the standard format but unfortunately we found that within a process, we were generating duplicate IDs if we created more than a certain amount. It didn’t take much to remedy the problem since Java introduced java.util.UUID in Java 1.5.

To create a universally unique ID in Java 1.5 or greater just call the static randomUUID method on the UUID class.

UUID.randomUUID().toString()

UUID has a wider range of possible values that I’m not concern of duplicate values or collisions for my application.


Leave a Reply