java.math.BigDecimal.hashCode()返回此BigDecimal的哈希碼。對於具有相等值和不同精度(例如4743.0和4743.00)的兩個BigDecimal對象,哈希碼通常將是不同的。
用法:
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:該程序將說明具有相等值但不同比例的兩個不同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://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#hashCode()
相關用法
- Java BigDecimal ulp()用法及代碼示例
- Java BigDecimal abs()用法及代碼示例
- Java BigDecimal plus()用法及代碼示例
- Java BigDecimal max()用法及代碼示例
- Java BigDecimal min()用法及代碼示例
- Java CopyOnWriteArrayList hashCode()用法及代碼示例
- Java FieldPosition hashCode()用法及代碼示例
- Java Collator hashCode()用法及代碼示例
- Java Vector hashCode()用法及代碼示例
- Java ParsePosition hashCode()用法及代碼示例
- Java RuleBasedCollator hashCode()用法及代碼示例
- Java MessageFormat hashCode()用法及代碼示例
- Java Map hashCode()用法及代碼示例
- Java Stack hashCode()用法及代碼示例
注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 BigDecimal hashCode() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。