Java-math-bigdecimal-hashcode

提供:Dev Guides
移動先:案内検索

Java.math.BigDecimal.hashCode()メソッド

説明

  • java.math.BigDecimal.hashCode()*は、このBigDecimalのハッシュコードを返します。 数値的には等しいが、スケールが異なる2つのBigDecimalオブジェクト(2.0や2.00など)は、通常、同じハッシュコードを持ちません。

宣言

以下は* java.math.BigDecimal.hashCode()*メソッドの宣言です。

public int hashCode()

オーバーライド

クラス Object のhashCode。

パラメーター

NA

戻り値

このメソッドは、BigDecimalオブジェクトのhashCode値を返します。

例外

NA

次の例は、math.BigDecimal.hashCode()メソッドの使用法を示しています。

package com.finddevguides;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

     //create 3 BigDecimal objects
      BigDecimal bg1, bg2, bg3;

     //create 3 int objects
      int i1, i2, i3;

      bg1 = new BigDecimal("125");
      bg2 = new BigDecimal("125.50");
      bg3 = new BigDecimal("125.80");

     //assign the HashCode value of bg1, bg2, bg3 to i1, i2, i3
     //respectively
      i1 = bg1.hashCode();
      i2 = bg2.hashCode();
      i3 = bg3.hashCode();

      String str1 = "HashCode of " + bg1 + " is " + i1;
      String str2 = "HashCode of " + bg2 + " is " + i2;
      String str3 = "HashCode of " + bg3 + " is " + i3;

     //print i1, i2, i3 values
      System.out.println( str1 );
      System.out.println( str2 );
      System.out.println( str3 );
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

HashCode of 125 is 3875
HashCode of 125.50 is 389052
HashCode of 125.80 is 389982