Java Utility Library

Java UUID - compareTo() Method



The java.util.UUID.compareTo() method is used to compare this UUID with the specified UUID. The first of two UUIDs is greater than the second if the most significant field in which the UUIDs differ is greater for the first UUID.

Syntax

public int compareTo(UUID val)

Parameters

val Specify the UUID to which this UUID is to be compared.

Return Value

Returns -1, 0 or 1 as this UUID is less than, equal to, or greater than val.

Exception

NA.

Example:

In the example below, the java.util.UUID.compareTo() method is used to compare two UUIDs.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating UUIDs
    UUID uid1 = UUID.fromString("d81e4f2e-cdf2-10e6-529b-7df92533d1cb");
    UUID uid2 = UUID.fromString("d81e4f2e-cdf2-10e6-529b-7df92533d1cb");

    //comparing uuids
    System.out.println("Comparing Two UUIDs: " + uid1.compareTo(uid2));  
  }
}

The output of the above code will be:

Comparing Two UUIDs: 0

❮ Java.util - UUID