Java Long.hashCode()
方法
原文:https://www.studytonight.com/java-wrapper-class/java-long-hashcode-method
hashCode()
方法是java.lang
包的Long
类的一部分。它用于返回给定输入的哈希代码。hashcode 被定义为与 Java 中的每个对象相关联的唯一原始整数值。
该方法覆盖了Object
类.
的方法
语法:
public int hashCode()
参数:
此方法中没有传递任何参数。
返回:
与Long
对象关联的等效原始整数值(哈希码)。
例 1:
这里,使用hashCode()
方法,编译器返回一个 hashcode。
import java.lang.Long;
public class StudyTonight
{
public static void main(String[] args)
{
Long n1 = 81L;
Long n2 = 27L;
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);
}
}
哈希码值:81 哈希码值:27
例 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);
Long n = sc.nextLong();
//Returning the hash code value for the object
int hv = n.hashCode();
System.out.println("Hash code Value for the given input is: " + hv);
}
catch(Exception e)
{
System.out.println("Invalid Input!!");
}
}
}
输入输入:89 给定输入的哈希码值为:89
- T3】输入:-552 给定输入的哈希码值为:551
- T6】输入:0x5334 无效输入!!
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。