Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient method to generate UUID String in JAVA (UUID.randomUUID().toString() without the dashes)

Tags:

java

uuid

random

I would like an efficient utility to generate unique sequences of bytes. UUID is a good candidate but UUID.randomUUID().toString() generates stuff like 44e128a5-ac7a-4c9a-be4c-224b6bf81b20 which is good, but I would prefer dash-less string.

I'm looking for an efficient way to generate a random strings, only from alphanumeric characters (no dashes or any other special symbols).

like image 408
Maxim Veksler Avatar asked Sep 27 '10 14:09

Maxim Veksler


People also ask

Can I remove dashes from UUID?

So, removing the dashes won't affect the uniqueness of the UUID. However, it may cause issues with libraries that expect the dashes as part of a UUID to validate it as a UUID.

What is UUID randomUUID () in Java?

The randomUUID() method is used to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.

How do you reduce the length of a UUID generated using randomUUID?

You can use base64 encoding and reduce it to 22 characters. If you use base94 you can get it does to 20 characters. If you use the whole range of valid chars fro \u0000 to \ufffd you can reduce it to just 9 characters or 17 bytes.


1 Answers

This does it:

public static void main(String[] args) {     final String uuid = UUID.randomUUID().toString().replace("-", "");     System.out.println("uuid = " + uuid); } 
like image 71
Steve McLeod Avatar answered Sep 28 '22 20:09

Steve McLeod