Java Long.hashCode(long)
方法
原文:https://www.studytonight.com/java-wrapper-class/java-long-hashcode-long-val-method
hashCode(long n)
方法兼容 Java 1.8 或更高版本,是java.lang
包的Long
类的一部分。它用于返回作为参数传递的长值的哈希代码。
此方法与Long
类的 Long**.hashCode()**
方法兼容。
语法:
public static int hashCode(long value)
参数:
该参数包括要生成哈希代码的长值。
返回:
与作为参数传递的长值关联的唯一整数值(哈希代码)。
例 1:
在这里,使用hashCode(long n)
函数,传递的 Long 值被转换为其各自的 hashcode。
import java.lang.Long;
public class StudyTonight
{
public static void main(String[] args)
{
int hv1 = Long.hashCode(27L); //generate the hashcode of the passed argument
int hv2 = Long.hashCode(81L);
System.out.println("Hash code Value is: " + hv1);
System.out.println("Hash code Value is: " + hv2);
}
}
哈希码值为:27 哈希码值为:81
例 2:
这里有一个用户定义的例子,任何使用这个代码的人都可以输入自己选择的值,并获得等效的哈希代码。
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the value: ");
Scanner sc = new Scanner(System.in);
Long i = sc.nextLong();
int hv = Long.hashCode(i); // Returning hash code value for this object
System.out.println("Hash code is: " + hv);
}
catch(Exception e)
{
System.out.println("Invalid Input!!");
}
}
}
输入值:855 哈希码为:855
- T3】输入值:-676 哈希码为:675 *输入值:0x588 无效输入!!
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。