Java中的BigDecimal hashCode()方法

这个 JAVA数学大十进制。hashCode() 返回的哈希代码 大十进制。对于两个具有相同值和不同比例(如4743.0和4743.00)的BigDecimal对象,哈希代码通常不相同。

null

语法:

public int hashCode()

参数: 此方法不接受任何参数。

返回值: 此方法返回的整数值等于BigDecimal对象的hashCode值。

例如:

Input : BigDecimal = 67891    
Output : Hashcode : 2104621

Input : BigDecimal = 67891.000
Output : Hashcode : 2104621003

下面的程序演示了BigDecimal类的hashCode()函数: 项目1:

// 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: 这个程序将说明两个不同的大小数的哈希代码是不同的。

// 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://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#hashCode()

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享