Java Float.hashCode(float)
方法
原文:https://www.studytonight.com/java-wrapper-class/java-float-hashcode-float-val-method
hashCode(float n)
方法兼容 Java 1.8 或更高版本,是java.lang
包的 Float 类的一部分。它用于返回作为参数传递的浮点数的哈希代码。
该方法与 Float 类的 Float.hashCode() 方法兼容。
语法:
public static int hashCode(float value)
参数:
参数包括要生成哈希代码的浮点值。
返回:
与作为参数传递的浮点值关联的唯一整数值(哈希代码)。
例 1:
这里,使用hashCode(float n)
函数,传递的浮点值被转换为其各自的 hashcode。
import java.lang.Float;
public class StudyTonight
{
public static void main(String[] args)
{
int hv1 = Float.hashCode(27.56930f); //generate the hashcode of the passed argument
int hv2 = Float.hashCode(-81.4889f);
System.out.println("Hash code Value is: " + hv1);
System.out.println("Hash code Value is: " + hv2);
}
}
哈希码值为:1104973293 哈希码值为:-1029506479
例 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);
float i = sc.nextFloat();
int hv = Float.hashCode(i); // Returning hash code value for this object
System.out.println("Hash code is: " + hv);
}
catch(Exception e)
{
System.out.println("Invalid Input!!");
}
}
}
输入值:5889.034 哈希码为:1169688646
- *输入值:—57.067 哈希码为:—1033616228
- *输入值:0x67 无效输入!!
实时示例:
在这里,您可以测试实时代码示例。您可以为不同的值执行示例,甚至可以编辑和编写您的示例来测试 Java 代码。