Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

UUID compareTo() Method in Java with Examples

  • Last Updated : 27 Dec, 2018

The compareTo() method of UUID class in Java is used to compare one UUID value with another specified UUID. It returns -1 if this UUID is less than the value, 0 if this UUID is equal to the value, and 1 if this UUID is greater than the value.

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

UUID_1.compareTo(UUID_2)

Parameters: The method takes one parameter UUID_2 to which the UUID_1 is to be compared.

Return Value: The method returns -1, 0 or 1 depending on whether this UUID_1 is less than, equal to or greater than the other UUID_2.



Below programs illustrate the working of compareTo() method:

Program 1: Checking if both the UUIDs are equal.




// Java code to illustrate compareTo() method
  
import java.util.*;
  
public class UUID_Demo {
    public static void main(String[] args)
    {
  
        // Creating two UUIDs
        UUID UUID_1
            = UUID
                  .fromString(
                      "5fc03087-d265-11e7-b8c6-83e29cd24f4c");
  
        UUID UUID_2
            = UUID
                  .fromString(
                      "5fc03087-d265-11e7-b8c6-83e29cd24f4c");
  
        // Displaying the UUID values
        System.out.println("UUID_1: "
                           + UUID_1.clockSequence());
        System.out.println("UUID_2: "
                           + UUID_2.clockSequence());
  
        // Comparing both the UUIDs
        System.out.println("Comparison Value: "
                           + UUID_1.compareTo(UUID_2));
    }
}
Output:
UUID_1: 14534
UUID_2: 14534
Comparison Value: 0

Program 2: When UUID_1 is less than UUID_2, the method returns -1.




// Java code to illustrate compareTo() method
  
import java.util.*;
  
public class UUID_Demo {
    public static void main(String[] args)
    {
  
        // Creating two UUIDs
        UUID UUID_1
            = UUID
                  .fromString(
                      "58e0a7d7-eebc-11d8-9669-0800200c9a66");
  
        UUID UUID_2
            = UUID
                  .fromString(
                      "5fc03087-d265-11e7-b8c6-83e29cd24f4c");
  
        // Displaying the UUID values
        System.out.println("UUID_1: "
                           + UUID_1.clockSequence());
        System.out.println("UUID_2: "
                           + UUID_2.clockSequence());
  
        // Comparing both the UUIDs
        System.out.println("Comparison Value: "
                           + UUID_1.compareTo(UUID_2));
    }
}
Output:
UUID_1: 5737
UUID_2: 14534
Comparison Value: -1

Program 3: When UUID_1 is greater than UUID_2, the method returns 1.




// Java code to illustrate compareTo() method
  
import java.util.*;
  
public class UUID_Demo {
    public static void main(String[] args)
    {
  
        // Creating two UUIDs
        UUID UUID_1
            = UUID
                  .fromString(
                      "5fc03087-d265-11e7-b8c6-83e29cd24f4c");
  
        UUID UUID_2
            = UUID
                  .fromString(
                      "58e0a7d7-eebc-11d8-9669-0800200c9a66");
  
        // Displaying the UUID values
        System.out.println("UUID_1: "
                           + UUID_1.clockSequence());
        System.out.println("UUID_2: "
                           + UUID_2.clockSequence());
  
        // Comparing both the UUIDs
        System.out.println("Comparison Value: "
                           + UUID_1.compareTo(UUID_2));
    }
}
Output:
UUID_1: 14534
UUID_2: 5737
Comparison Value: 1



My Personal Notes arrow_drop_up
Recommended Articles
Page :