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
相关用法
- Java UUID variant()用法及代码示例
- Java UUID fromString()用法及代码示例
- Java UUID hashCode()用法及代码示例
- Java UUID node()用法及代码示例
- Java UUID toString()用法及代码示例
- Java UUID timestamp()用法及代码示例
- Java UUID getLeastSignificantBits()用法及代码示例
- Java UUID nameUUIDFromBytes()用法及代码示例
- Java UUID version()用法及代码示例
- Java UUID equals()用法及代码示例
- Java UUID clockSequence()用法及代码示例
- Java UUID getMostSignificantBits()用法及代码示例
- Java LocalTime compareTo()用法及代码示例
- Java FloatBuffer compareTo()用法及代码示例
- Java Charset compareTo()用法及代码示例
注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 UUID compareTo() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。