📜  Java中的 UUID compareTo() 方法及示例

📅  最后修改于: 2022-05-13 01:55:16.189000             🧑  作者: Mango

Java中的 UUID compareTo() 方法及示例

Java中UUID 类compareTo()方法用于将一个 UUID 值与另一个指定的 UUID 进行比较。如果此 UUID 小于该值,则返回 -1,如果此 UUID 等于该值,则返回 0,如果此 UUID 大于该值,则返回 1。

句法:

UUID_1.compareTo(UUID_2)

参数:该方法采用一个参数UUID_2 ,与 UUID_1 进行比较。

返回值:该方法返回-1、0 或 1,具体取决于此 UUID_1 是否小于、等于或大于另一个 UUID_2。

下面的程序说明了 compareTo() 方法的工作:

程序 1:检查两个 UUID 是否相等。

// 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));
    }
}
输出:
UUID_1: 14534
UUID_2: 14534
Comparison Value: 0

方案二:当UUID_1小于UUID_2时,方法返回-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));
    }
}
输出:
UUID_1: 5737
UUID_2: 14534
Comparison Value: -1

方案3:当UUID_1大于UUID_2时,方法返回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));
    }
}
输出:
UUID_1: 14534
UUID_2: 5737
Comparison Value: 1