Java中的 BigDecimal hashCode() 方法
Java.math.BigDecimal.hashCode()返回此BigDecimal 的哈希码。对于具有相同值和不同比例的两个 BigDecimal 对象(如 4743.0 和 4743.00),哈希码通常不会相同。
句法:
public int hashCode()
参数:此方法不接受任何参数。
返回值:此方法返回一个整数值,该整数值等于 BigDecimal 对象的 hashCode 值。
例子:
Input : BigDecimal = 67891
Output : Hashcode : 2104621
Input : BigDecimal = 67891.000
Output : Hashcode : 2104621003
下面的程序说明了 BigDecimal 类的 hashCode()函数:
方案一:
// Java program to demonstrate hashCode() method
import java.io.*;
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating a BigDecimal object
BigDecimal b;
// Assigning value
b = new BigDecimal(4743);
System.out.print("HashCode for " + b + " is ");
System.out.println(b.hashCode());
}
}
输出:
HashCode for 4743 is 147033
程序 2:该程序将说明具有相同值但不同比例的两个不同 BigDecimal 的哈希码将不同。
// Java program to demonstrate hashCode() method
import java.io.*;
import java.math.*;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigDecimal objects
BigDecimal b1, b2;
// Assigning values
b1 = new BigDecimal("4743");
b2 = new BigDecimal("4743.000");
int i1, i2;
i1 = b1.hashCode();
i2 = b2.hashCode();
if (i1 == i2) {
System.out.println("HashCodes of " +
b1 + " and " + b2 + " are equal.");
System.out.println("Both their HashCodes are " +
i1 + ".");
}
else {
System.out.println("HashCodes of " + b1 + " and "
+ b2 + " are not equal.");
System.out.println("HashCodes of " + b1 + " is "
+ i1 + " and " + b2 + " is " + i2 + ".");
}
}
}
输出:
HashCodes of 4743 and 4743.000 are not equal.
HashCodes of 4743 is 147033 and 4743.000 is 147033003.
参考: https: Java/math/BigDecimal.html#hashCode()