当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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


相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 UUID equals() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。