Efficient method for generating UUID strings in JAVA (UUID.randomUUID (). ToString () without dash)

I would like an efficient utility to generate unique byte sequences. UUID is a good candidate, but UUID.randomUUID().toString() generates material like 44e128a5-ac7a-4c9a-be4c-224b6bf81b20 , which is good until you need to send it over HTTP, in which case the dash needs to be removed.

I am looking for an efficient way to generate random strings, only from alphanumeric characters (without dashes or any other special characters).

+81
java uuid random
Sep 27 '10 at 14:05
source share
7 answers

I finished writing something of my own, based on the implementation of UUID.java. Note that I am not generating a UUID , but just a random 32-bit hexadecimal string in the most efficient way I could think of.

Implementation

 import java.security.SecureRandom; import java.util.UUID; public class RandomUtil { // Maxim: Copied from UUID implementation :) private static volatile SecureRandom numberGenerator = null; private static final long MSB = 0x8000000000000000L; public static String unique() { SecureRandom ng = numberGenerator; if (ng == null) { numberGenerator = ng = new SecureRandom(); } return Long.toHexString(MSB | ng.nextLong()) + Long.toHexString(MSB | ng.nextLong()); } } 

Using

 RandomUtil.unique() 

Test

Some of the inputs I tested to make sure they work:

 public static void main(String[] args) { System.out.println(UUID.randomUUID().toString()); System.out.println(RandomUtil.unique()); System.out.println(); System.out.println(Long.toHexString(0x8000000000000000L |21)); System.out.println(Long.toBinaryString(0x8000000000000000L |21)); System.out.println(Long.toHexString(Long.MAX_VALUE + 1)); } 
+7
Sep 28 '10 at 10:22
source share

It does:

 public static void main(String[] args) { final String uuid = UUID.randomUUID().toString().replace("-", ""); System.out.println("uuid = " + uuid); } 
+138
Sep 27 '10 at 14:12
source share

Hyphens do not need to be removed from the HTTP request, as you can see in the URL of this stream. But if you want to prepare a well-formed URL without data dependency, you should use URLEncoder.encode (String data, String encoding) instead of changing the standard form of your data. For a string representation, the UUID dash is normal.

+26
Sep 27 2018-10-09T00:
source share

I used the JUG (Java UUID Generator) to generate a unique identifier. It is unique to the JVM. Pretty good to use. Here is the code for your reference:

 private static final SecureRandom secureRandom = new SecureRandom(); private static final UUIDGenerator generator = UUIDGenerator.getInstance(); public synchronized static String generateUniqueId() { UUID uuid = generator.generateRandomBasedUUID(secureRandom); return uuid.toString().replaceAll("-", "").toUpperCase(); } 

You can download the library from: https://github.com/cowtowncoder/java-uuid-generator

+9
Sep 27 '10 at 22:00
source share

I am amazed that many lines replace UUID ideas. How about this:

 UUID temp = UUID.randomUUID(); String uuidString = Long.toHexString(temp.getMostSignificantBits()) + Long.toHexString(temp.getLeastSignificantBits()); 

This is a quick way to do this, since the whole toString () UUID is already more expensive, not to mention the regular expression that needs to be parsed and executed or replaced with an empty string.

+6
Jun 23 '16 at 2:56
source share

A simple solution is

 UUID.randomUUID().toString().replace("-", "") 

(Like existing solutions, this only avoids calling String # replaceAll . There is no need to replace the regular expression here, so String # replace feels more natural, although technically it still does with regular expressions. Given that generating UUIDs is more expensive than replacement should not be a significant difference in lead time.)

Using the UUID class is probably fast enough for most scenarios, although I would expect some specialized handwriting option that does not require postprocessing to be faster. In any case, the bottleneck of general computation will usually be a random number generator. In the case of the UUID class, it uses SecureRandom .

Which random number generator to use is also a trade-off, which depends on the application. If it is security sensitive, SecureRandom is generally recommended. Otherwise, ThreadLocalRandom is an alternative (faster than SecureRandom or old Random , but not cryptographically secure).

+1
Sep 07 '16 at 23:55
source share

I use org.apache.commons.codec.binary.Base64 to convert the UUID to a unique string with a unique string of 22 characters and has the same uniqueness as the UUID.

I sent my code to Save UUID as base64 string

0
Aug 08 '13 at 9:05
source share



All Articles