Java Double.hashCode()方法

原文:https://www.studytonight.com/java-wrapper-class/java-double-hashcode-method

hashCode()方法是java.lang包的Double类的一部分。它用于返回给定输入的哈希代码。hashcode 被定义为与 Java 中的每个对象相关联的唯一原始整数值。

此方法返回的结果是long整数位表示的两半的异或,并返回与doubleToLongBits()方法返回的值相同的值。该方法优先于Object class.的方法

语法:

public int hashCode()

参数:

此方法中没有传递任何参数。

返回:

与双对象关联的等效基本整数值(哈希代码)。

例 1:

这里,使用hashCode()方法,Double 对象被转换成其各自的 hashcode。

import java.lang.Double;

public class StudyTonight
{  
    public static void main(String[] args)  
    {  

        Double n1 = 81.69445;
        Double n2 = -27.90355;

        int hv1 = n1.hashCode();//Returning the hash code value for the object n1
        int hv2 = n2.hashCode();//Returning the hash code value for the object n2
        System.out.println("Hash code Value : " + hv1);
        System.out.println("Hash code Value : " + hv2);  
    }  
}

哈希码值:-1640120014 哈希码值:-843077042

例 2:

这里有一个用户定义的例子,任何使用这个代码的人都可以输入自己选择的值,并获得等效的哈希代码。

import java.util.Scanner;  

public class StudyTonight
{  
    public static void main(String[] args)
      {  
        try
        {
          System.out.print("Enter input: ");  
          Scanner sc = new Scanner(System.in);         
          Double n = sc.nextDouble();  
          int hv = n.hashCode(); //Returning the hash code value for the object 
          System.out.println("Hash code Value for the given input is: " + hv);

        }
        catch(Exception e)
        {
          System.out.println("Invalid Input!!");
        }

      }  
}

输入:89.55 给定输入的哈希码值为:1936019456

                                                                                                    • *输入:-77.09 给定输入的哈希码值为:1326411060
                                                                                                                                          • *输入:0x556 无效输入!

实时示例:

在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。