Java UUID Generator Example
Hello readers, in this tutorial, we are generating the UUID using the Java and Java UUID Generator API.
1. Introduction
UUID / GUID (Universally / Globally Unique Identifier) is frequently used in programming. Some of its usages are for creating random file names, session id in the web application, transaction id and for record’s primary keys in the database (i.e. replacing the sequence or auto-generated number).
UUID (i.e. Universally Unique Identifier) class is a part of the java.util
package and represents an immutable universally unique identifier with a 128 bit
number string value. It is represented in 32
Hexadecimal characters and is mixed with 4 dashes (i.e. -
) characters. UUID also was known as GUID which stands for Globally Unique Identifier.
Following are the important methods available on the UUID class.
String toString()
: This method will return a string object representing the UUIDint variant()
: This method will return the variant number associated with the UUIDint version()
: This method will return the version number associated with the UUID
Now, open up the Eclipse Ide and let’s see a few examples of how to generate the UUID object in Java!
2. Java UUID Generator Example
2.1 Tools Used
We are using Eclipse Kepler SR2, JDK 8 and Maven. Having said that, we have tested the code against JDK 1.7 and it works well.
2.2 Project Structure
Firstly, let’s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!
2.3 Project Creation
This section will demonstrate on how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to File -> New -> Maven Project
.
In the New Maven Project window, it will ask you to select project location. By default, ‘Use default workspace location’ will be selected. Select the ‘Create a simple project (skip archetype selection)’ checkbox and just click on next button to proceed.
It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in the below image. The version number will be by default: 0.0.1-SNAPSHOT
.
Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a pom.xml
file will be created. It will have the following code:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>JavaUuidEx</groupId> <artifactId>JavaUuidEx</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> </project>
Developers can start adding the dependencies that they want to such as UUID Generator etc. Let’s start building the application!
3. Application Building
Below are the steps involved in developing this application.
3.1 Maven Dependencies
Here, we specify the dependencies for the UUID Generator. The rest dependencies will be automatically resolved by the Maven framework and the updated file will have the following code:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>JavaUuidEx</groupId> <artifactId>JavaUuidEx</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.fasterxml.uuid</groupId> <artifactId>java-uuid-generator</artifactId> <version>3.1.4</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> </build> </project>
3.2 Java Class Creation
Let’s create the required Java files. Right-click on the src/main/java
folder, New -> Package
.
A new pop window will open where we will enter the package name as: com.jcg.java
.
Once the package is created in the application, we will need to create the 2
different implementation classes in order to generate the UUID object. Right-click on the newly created package: New -> Class
.
A new pop window will open and enter the file name as: UuidExample
. The utility class will be created inside the package: com.jcg.java
.
Repeat the step (i.e. Fig. 7) and enter the filename as: JugUUIDExample
. The main class will be created inside the package: com.jcg.java
.
3.2.1 Implementation of Java UUID Class
To generate the UUID object in Java, developers can use the java.util.UUID
class which was introduced in JDK 1.5. Let’s see the simple code snippet that follows this implementation.
UuidExample.java
package com.jcg.java; import java.util.UUID; public class UuidExample { public static void generateRandomUuid(int num) { /***** Generate Version 4 UUID - Randomly Generated UUID *****/ UUID uid = null; for(int i=1; i<num; i++) { /***** Generating Random UUID's *****/ uid= UUID.randomUUID(); System.out.println("Generated UUID?= " + uid.toString() + ", UUID Version?= " + uid.version() + ", UUID Variant?= " + uid.variant() + "\n"); } } public static void main(String[] args) { int num = 4; generateRandomUuid(num); } }
3.2.2 Implementation of Java UUID Generator Class
Developers can use the Java UUID Generator (JUG) utility to generate the UUID object. Let’s see the simple code snippet that follows this implementation.
JugUUIDExample.java
package com.jcg.java; import java.util.UUID; import com.fasterxml.uuid.Generators; public class JugUUIDExample { public static void generateRandomUuid() { /***** Generate Version 1 UUID - Time-Based UUID *****/ UUID uuid1 = Generators.timeBasedGenerator().generate(); System.out.println("Generated Version 1 UUID?= " + uuid1.toString() + ", UUID Version?= " + uuid1.version() + ", UUID Variant?= " + uuid1.version() + "\n"); /***** Generate Version 4 UUID - Randomly Generated UUID *****/ UUID uuid2 = Generators.randomBasedGenerator().generate(); System.out.println("Generated Version 4 UUID?= " + uuid2.toString() + ", UUID Version?= " + uuid2.version() + ", UUID Variant?= " + uuid2.version()); } public static void main(String[] args) { generateRandomUuid(); } }
Do note, developers will have to use the java-uuid-generator
maven dependency for generating the UUID object using the Java UUID Generator class.
4. Run the Application
To run the application, Right-click on the UuidExample
class -> Run As -> Java Application
. Developers can debug the example and see what happens after every step!
5. Project Demo
The application shows the following logs as output for the UuidExample.java
. This class will randomly generate a Version 4 UUID object.
Generated UUID?= 771cdd2e-e524-4218-b0da-21d0fd9a5307, UUID Version?= 4, UUID Variant?= 2 Generated UUID?= 16caf416-2434-42f5-ac5c-031bc3787247, UUID Version?= 4, UUID Variant?= 2 Generated UUID?= a42ac726-f54a-4094-a4d2-b7d1c70f9237, UUID Version?= 4, UUID Variant?= 2
The JugUUIDExample.java
will generate a Version 1 and Version 4 UUID object and shows the following logs as output.
Generated Version 1 UUID?= c1336282-f08b-11e7-b344-c74db8d97c98, UUID Version?= 1, UUID Variant?= 1 Generated Version 4 UUID?= f803c0bc-ae39-4d1b-83c0-bcae392d1bd5, UUID Version?= 4, UUID Variant?= 4
That’s all for this post. Happy Learning!!
6. Conclusion
That’s all for Java UUID class and developers can use it to create the unique identifiers for their application. I hope this article served you whatever you were looking for.
7. Download the Eclipse Project
This was an example of Java UUID Generator for the beginners.
You can download the full source code of this example here: JavaUuidEx