Java UUID hashCode()方法及实例

Java UUID hashCode()方法及实例

Java中UUID类的hashCode()方法通常用于获取UUID的哈希代码值。

语法

public int hashCode()

参数: 该方法不接受任何参数。

返回值: 该方法返回一个整数值,即该UUID实例的hashCode值。

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

程序1 :

// Java code to illustrate hashCode() 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");
  
        // Displaying the UUID
        System.out.println("UUID: "
                           + UUID_1);
  
        // Displaying the hashCode value
        System.out.println("The hashCode value is: "
                           + UUID_1.hashCode());
    }
}

输出:

UUID: 58e0a7d7-eebc-11d8-9669-0800200c9a66
The hashCode value is: 3744873

程序2

// Java code to illustrate hashCode() 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");
  
        // Displaying the UUID
        System.out.println("UUID: "
                           + UUID_1);
  
        // Displaying the hashCode Value
        System.out.println("The hashCode value is: "
                           + UUID_1.hashCode());
    }
}

输出:

UUID: 5fc03087-d265-11e7-b8c6-83e29cd24f4c
The hashCode value is: -1447957042

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程