Java UUID equals()方法及实例

Java UUID equals()方法及实例

Java中 UUID类equals() 方法是用来检查一个UUID与另一个UUID是否相等。如果两个UUID的位数完全相同,该方法返回True。

语法

public boolean equals(Object uuidObj)

参数: 该方法需要一个参数uuidObj,UUID_1将与之进行比较。

返回值: 如果两个UUID都是相同的,该方法返回true,否则返回false。

下面的程序说明了equals()方法的工作原理。

程序1 :

// Java code to illustrate equals() 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("Are both UUID equal: "
                           + UUID_1.equals(UUID_2));
    }
}

输出:

UUID_1: 14534
UUID_2: 14534
Are both UUID equal: true

程序2

// Java code to illustrate equals() 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("Are both UUID equal: "
                           + UUID_1.equals(UUID_2));
    }
}

输出:

UUID_1: 14534
UUID_2: 5737
Are both UUID equal: false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程