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

  • A+
Category:Languages

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?

 


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 128bit 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 as Strings to external systems such as external APIs, databases, and things of that nature. They might be expecting the dashes to be there.

Also how unique each Java UUID?

It's as good as Java's random number generator. As long as there's nothing screwy with it, each UUID can be considered unique for all practical purposes - "The UUID is generated using a cryptographically strong pseudo random number generator." (from the Javadoc)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: