Java UUID compareTo()方法及实例

Java UUID compareTo()方法及实例

Java中 UUID类compareTo() 方法是用来比较一个UUID值和另一个指定的UUID。如果该UUID小于该值,则返回-1;如果该UUID等于该值,则返回0;如果该UUID大于该值则返回1。

语法

UUID_1.compareTo(UUID_2)

参数: 该方法需要一个参数UUID_2,UUID_1要与之比较。

返回值: 该方法根据该UUID_1是否小于、等于或大于另一个UUID_2而返回-1、0或1。

下面的程序说明了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

程序2: 当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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程