Java Unique Number Generator - UUID or GUID with examples

In this blog post, We are going to learn java Unique Number Generator- UUID or GUID with examples.

You can also check my previous articles on UUI.

Java Unique Number Generator - UUID

The universally unique identifier is a unique string to represent information in software applications.

There are two types of unique identifiers.

  • UUID - universally unique identifier,

  • GUID globally unique identifier,

    The difference between UUID and GUID, Both are returning 126 bits,

GUID is Microsoft’s implementation of UUID functionality. Both generate 16 bytes hexadecimal separated by a hyphen in four groups.

UUID or GUID uses in the following in real-time

  • Used to represent a primary key in a database table
  • can be used as Session ID in web applications, Transaction Id in enterprise applications.
  • Can also be used as a unique identifier for doing CRUD operations in REST API applications.
  • Represents to identify big data spread across multiple servers.
  • Naming static resources for storing and retrieving files.

Java provides java.util.UUID class for providing UUID implementation in java

java.util.UUID class

This class is specified as an immutable universally unique identifier (UUID). This class has the following notes about it

  • Generates 128 bit or 16 bytes that contain hexadecimal values
  • Implements Serializable, Comparable interfaces
  • Extends Object class.

There are multiple variants for creating a UUID. It has methods for operating the Leach-Salz variant.

However, We can create multiple variants using the constructor.

Constructor:

UUID(long mostSigBits, long leastSigBits)

It has only one constructor which takes two parameters to create a new UUID mostSigBits - most significant bits 64 bits of UUID leastSigBits - least significant 64 bits of UUID

UUID has four types of versions

  • Version 1 represents time-based - UUID V1
  • Version 2 represents DCE security - UUID V2
  • Version 3 represents name-based - UUID V3
  • Version 4 represents Random generated - - UUID V4

Example

The below example generates a UUID V4 string in java.

import java.util.UUID;

public class UUIDDemo {

 public static void main(String[] args) {

  UUID uuid = UUID.randomUUID();
  String uuidString = uuid.toString();
  System.out.println(uuid.variant());  // 2
  System.out.println(uuid.version());  // 4
  System.out.println(uuidString);  //3d795ac3-2cea-4ed2-92d8-3d71a2539cf2

 }
}

java-uuid-generator maven

In maven applications, java-uuid-generator is a dependency from com.fasterxml.UUID. It is a java library for generating UUID with all versions.

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

The below example generates the following things.

  • UUID V1 Generation using timebase generator
  • UUID V3 Generation using UUID.nameUUIDFromBytes method as well as name-based
  • UUID V4 Generation using randomBasedGenerator
  • UUID V5 Generation using SHA digester
import java.util.UUID;

import com.fasterxml.uuid.Generators;

public class HelloWorld {
 public static void main(String[] args) {
  // UUID V1 generation
  UUID v1 = Generators.timeBasedGenerator().generate();
  System.out.println("UUID V1 value: " + v1.toString());
  System.out.println("version  : " + v1.version());
  System.out.println("variant  : " + v1.variant());
  // UUID V3 generation
  //  create byte array and initialize it
  byte[] byteArray = { 0, 1, 4 };
  // Create uuid from byte array
  UUID v3 = UUID.nameUUIDFromBytes(byteArray);
  System.out.println("UUID V4 value: " + v3.toString());
  System.out.println("version  : " + v3.version());
  System.out.println("variant  : " + v3.variant());
  // UUID V4 generation
  UUID v4 = Generators.randomBasedGenerator().generate();
  System.out.println("UUID V4 value: " + v4.toString());
  System.out.println("version  : " + v4.version());
  System.out.println("variant  : " + v4.variant());

  // UUID V5 generation
  UUID v5 = Generators.nameBasedGenerator().generate("cloudhadoop.com");
  System.out.println("UUID V5 value: " + v5.toString());
  System.out.println("version  : " + v5.version());
  System.out.println("variant  : " + v5.variant());


 }
}

The output of the above program code is

UUID V1 value: 04272c28-e69d-11e8-be73-df7930994371
version  : 1
variant  : 2
UUID V4 value: be489ef3-af30-3d20-b50a-5c504ecc5294
version  : 3
variant  : 2
UUID V4 value: 1c4e3ff8-bf58-4081-8e3f-f8bf58908109
version  : 4
variant  : 2
UUID V5 value: cdecd331-e6c2-5e7f-ad1d-3ee766052560
version  : 5
variant  : 2

How to create UUID from a String

UUID class provides fromString() method which generates UUID from a given string. This is an example for Converting String to UUID. This method throws Exception in thread “main” java.lang.IllegalArgumentException: Invalid UUID string: 150e064ff8d6, if string is not 16 bytes in length


import java.util.UUID;

public class fromStringExample {

 public static void main(String[] args) {

  UUID uuid = UUID.fromString("d4578015-0e06-4ff8-9780-150e064ff8d6");
  System.out.println("UUID  value: " + uuid.toString());
  System.out.println("version  : " + uuid.version());
  System.out.println("variant  : " + uuid.variant());
 }

}

The output of the above code is

UUID  value: d4578015-0e06-4ff8-9780-150e064ff8d6
version  : 4
variant  : 2
UUID class methods

UUID class methods

The follow methods are available in java.util.Util class

  • clockSequence - this method returns clock sequence for given UUID. It throws Exception in thread “main” java.lang.UnsupportedOperationException: Not a time-based UUID.if it is not time-based UUID version
  • getLeastSignificantBits - This method returns least significant 64 bits of given UUID
  • getMostSignificantBits() - This method returns m significant 64 bits of given UUID
  • nameUUIDFromBytes(bytearray) - this method returns UUID V3 for given byte array
  • node()- This method return node value of given UUID
  • timestamp() - This return timestamp of given UUID
  • randomUUID - This static method generates random UUID

Other UUID libraries

There are various UUID libraries available for the java language