UUID Guide in Java

What is UUID ?

I have come across the usage of UUID in various areas of my projects, which has piqued my curiosity to gain a deeper understanding of it.

UUID stands for ‘Universally Unique Identifier’. It is a class found in the java.util package and is utilized to create unique identifiers.

UUIDs are commonly needed in distributed environments where we need to generate unique keys that do not overlap with each other.

A UUID is a 128-bit value that is expressed as a hexadecimal string, usually divided into groups using hyphens (-)

1
2
Example:
xxxxxxxx-xxxx-Vxxx-Nxxx-xxxxxxxxxxxx.

The “V” indicates the version of the UUID format, and the “N” indicates the variant.

1
83247608-e126-4b1f-a4dd-e530f29db3c3

Common use cases for UUID:

  1. UUID is widely used to generate primary key of the entity classes
  2. To uniquely identify a transaction or business flow id in distributed systems
  3. To generate unique identifiers for session and cookies in web applications
  4. To generate random data for multiple test cases

How to generate UUID programmatically in Java?

UUID is a class that represents an immutable universally unique identifier (UUID).

A UUID represents a 128-bit value.

To get UUID we need to call static factory method randomUUID.

The UUID is generated using a cryptographically strong pseudo random number generator.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.harishgowda.demo;

import java.util.UUID;

public class UUIDDemo {

    public static void main(String[] args) {

         UUID uuid=  UUID.randomUUID();
        System.out.println(uuid.toString());

    }
}

Output is:

1
1f6ed84b-ac8a-4665-8886-3cadf25cfb1d

Getting UUID based on string

Creates a UUID from the string standard representation as described in the toString method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.harishgowda.demo;

import java.util.UUID;

public class UUIDDemo {

    public static void main(String[] args) {

         UUID uuid=  UUID.fromString("5f8ed84b-bc8a-4665-8886-3cadf25a0000");
        System.out.println(uuid.toString());

    }
}

Output:

1
5f8ed84b-bc8a-4665-8886-3cadf25a0000

Getting UUID from bytes

We can also create UUID from bytes by calling nameUUIDFromBytes method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.harishgowda.demo;

import java.util.UUID;

public class UUIDDemo {

    public static void main(String[] args) {

        byte[] b = {5, 10, 15};
        UUID uuid1=  UUID.nameUUIDFromBytes(b);
        System.out.println(uuid1.toString());
    }
}
1
aa8451bb-900e-3cda-9c2c-2143e374a6d1

Comparing 2 UUID’s

We can compare 2 UUID’s by calling compareTo() method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.harishgowda.demo;

import java.util.UUID;

public class UUIDDemo {

    public static void main(String[] args) {

         UUID uuid1=  UUID.randomUUID();
         UUID uuid2=  UUID.randomUUID();

        System.out.println("uuid1: "+uuid1.toString());
        System.out.println("uuid2: "+uuid2.toString());


        int compare = uuid1.compareTo(uuid2);
        if(compare==1)
            System.out.println("uuid1 is greater than uuid2");
        else if(compare==-1)
            System.out.println("uuid1 is smaller than uuid2");
        else if(compare==0)
            System.out.println("uuid1 == uuid2");

    }
}
1
2
3
uuid1: 1bf45270-f1a4-488d-b4d6-0f3b854d29a5
uuid2: 51e8e220-093a-4e07-b6c3-c084a0d1d23d
uuid1 is smaller than uuid2

Thus with the above examples we have seen that UUIDs ensures an identifier will be unique across different systems and over time.