1. Overview

In this article, We will discuss how to generate UUID in Java. We will also discuss different ways to generate random UUID, Time-based UUID, and Name-based UUID.

Full Form of UUID is Universal Unique Identifier, UUID is 128 bit unique identifier based on Object or Entity. UUID is a combination of 8-4-4-4-12 characters and looks like:

94f9dfc5-fca1-466d-981e-eb45a0aecccc

Primary purpose of UUID is to generate value based on Object. We can use random UUID for unique identifier of any Entity as primary key in database because it will give us guarantee that it will never repeat and it will always be unique.

2. Different ways to generate UUID in Java

UUID contains version and version and its type has the following meaning:

1    Time-based UUID
2    DCE security UUID
3    Name-based UUID
4    Randomly generated UUID

2.1 Generate random UUID

Java has a java.util.UUID utility class using that we can generate random UUID. java.util.UUID only provide ways to generate Random UUID, For timebase and name based UUID we require third party library.

Example

import java.util.UUID;
public class Demo {
    public static void main(String ... args){
       UUID uuid = UUID.randomUUID();
       System.out.println("Random UUID :" + uuid.toString());
       System.out.println("UUID version :" + uuid.version());    // 4 will be for random
    }
}

NOTE: UUID.timestamp() is a method but that only work for time base UUID (version 1), If we try to call using random UUID then it will throw an exception like: java.lang.UnsupportedOperationException: Not a time-based UUID

Output

Random UUID :e3d7eb8c-4bb9-4fc3-a84b-8f0e23eaf504
UUID version :4

2.2 Generate Timebase UUID

Generators.timeBasedGenerator() will generate UUID based on time/location and its version will be 1. We require  com.fasterxml.uuid Maven or Gradle dependency:

<dependency>
       <groupId>com.fasterxml.uuid</groupId>
       <artifactId>java-uuid-generator</artifactId>
       <version>3.1.4</version>
</dependency>

Example

import com.fasterxml.uuid.Generators;
public class Demo {
    public static void main(String ... args){
        UUID timebaseUUID =  Generators.timeBasedGenerator().generate();      // it will geneate timebased UUID
        System.out.println("Time based UUID :"+timebaseUUID.toString());      // UUID string 
        System.out.println("UUID version : "+timebaseUUID.version());         // 1 - version
        System.out.println("UUID Node : "+timebaseUUID.node());               // IEEE 802 address of mechine that generate value  
        System.out.println("UUID Timestamp : "+timebaseUUID.timestamp());     // 60 bit timestamp
    }
}

Output

Time based UUID :199c9590-be30-11e8-9995-e3730f2b3d7d
UUID version : 1
UUID Node : 250083315236221
UUID Timestamp : 137568902432200080

2.3 Generate name based UUID

Generators.nameBasedGenerator() generate UUID based on passed String. If we passed the same String then it will generate the same UUID second time also.

 <dependency>
            <groupId>com.fasterxml.uuid</groupId>
            <artifactId>java-uuid-generator</artifactId>
            <version>3.1.4</version>
</dependency>

Example

import com.fasterxml.uuid.Generators;
import java.util.UUID;
public class Demo {
    public static void main(String ... args){
        UUID namebaseUUID =  Generators.nameBasedGenerator().generate("JavaDeveloperZone");              // it will geneate name based UUID based on passed name
        System.out.println("Name based UUID :"+namebaseUUID .toString());                                // UUID string  
        System.out.println("UUID version : "+namebaseUUID .version());                                   // 5 - version
    }
}

Output

Name based UUID :9a5d9271-ee34-5a4d-bd8a-d649705d0cc5
UUID version : 5

3.3 Generate random UUID using com.fasterxml.uuid

com.fasterxml.uuid also provide a way to generate Random UUID, Generators.randomBasedGenerator() will return a generator using that we can generate random UUID.

Example

import com.fasterxml.uuid.Generators;
import java.util.UUID;
public class Demo {
    public static void main(String ... args){
        UUID randomUUID =  Generators.randomBasedGenerator().generate();
        System.out.println("Random UUID :"+randomUUID.toString());
        System.out.println("UUID version : "+randomUUID.version());
    }
}

Output

Random UUID :48743e60-2a53-4908-b43e-602a53090849
UUID version : 4

3. Conclusion

In this article, We learn different types of UUID like time base, name base, and random UUID and ways to generate those UUIDs using java.util.UUID and com.fasterxml.uuid third party java library.

4. References

Was this post helpful?

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *