How safe is it to remove the "-" in a randomly generated UUID?

quarks picture quarks · Aug 13, 2018 · Viewed 11k times · Source

I have this code:

String uuid = UUID.randomUUID().toString().replace("-", "");

How safe is it to remove the "-" in the generated UUID? Would removing it defeat the purpose of it being globally unique and make the generated UUID prone to collisions?

Answer

Malt picture Malt · Aug 13, 2018

how safe if is to remove the "-" in the generated UUID

It's 100% safe since the dashes aren't part of the value. The String UUID is a hex representation of a 128 bit value. The dashes are there just for display purposes so UUIDs will be a bit easier on the eyes.

Just be careful when passing UUIDs in String form to external systems such as external APIs, databases, and things of that nature. They might be expecting the dashes to be there.