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

Maxim Veksler picture Maxim Veksler · Sep 27, 2010 · Viewed 261.6k times · Source

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

Answer

Steve McLeod picture Steve McLeod · Sep 27, 2010

This does it:

public static void main(String[] args) {
    final String uuid = UUID.randomUUID().toString().replace("-", "");
    System.out.println("uuid = " + uuid);
}