In this example bigdecimal class hashCode() method working is shown. Method returns integer values, both negative & positive. For every number method generates different hash codes.
Java BigDecimal hashCode example
In this example bigdecimal class hashCode() method working is shown. Method returns integer values, both negative & positive. For every number method generates different hash codes.
Method generated hash codes does not match when similar values possess different scales.
Method generates NumberFormatException when it finds any String character value instead of a number value.
Syntax for using the method: public int
hashCode()
System.out.println(bigdecimal_objectName.hashCode());
or
int i = (this.object).hashCode();
Java_BigDecimal_hashCode.java
import java.math.BigDecimal;
import java.text.NumberFormat;
public class c {
public static void main(String args[]) {
NumberFormat num = NumberFormat.getInstance();
num.setMinimumFractionDigits(3);
BigDecimal buf_0 = new BigDecimal("rose".length()),
buf_1 = new BigDecimal("india".length());
System.out.println("BigDecimal object buf_0 value" +
" :" + buf_0
+ "\nBigDecimal object buf_1 value :" + buf_1);
System.out.println("\nhash code generated for buf_0 " +
"object value : " + buf_0.hashCode()
+ "\nhash code generated for buf_1 object value : "
+ buf_1.hashCode());
BigDecimal x = new BigDecimal(10.32455666888),
y = new BigDecimal(10.32455666888),
ray[] = { x, y };
int i = ray[0].hashCode(),
j = ray[1].hashCode();
System.out.println("\nhash code generated for x " +
"object value : " + i);
System.out.println("hash code generated for y " +
"object value : : " + j);
System.out.println("\n");
boolean evaluate_0 = x.equals(y);
if (evaluate_0 == true) {
System.out.println(x.equals(y) + " : objects x " +
"& y are equal");
} else
System.out.println(x.equals(y) + " : objects x & " +
"y are not equal");
x = new BigDecimal(num.format(x));
x = new BigDecimal(num.format(y));
System.out.println("\nResult after formating");
boolean evaluate_1 = x.equals(y);
if (evaluate_1 == true) {
System.out.println(x.equals(y) + " : objects x & " +
"y are equal");
} else
System.out.println(x.equals(y) + " : objects x & y" +
" are not equal");
}
}
|
Download the code