Java - Long hashCode() 方法



描述

Java Long hashCode() 方法返回此 Long 的雜湊碼。

宣告

以下是java.lang.Long.hashCode() 方法的宣告

public int hashCode()

引數

返回值

此方法返回此物件的雜湊碼值,等於此 Long 物件表示的原始長整型值。

異常

獲取具有正值的 Long 的雜湊碼示例

以下示例演示瞭如何使用 Long hashCode() 方法獲取長整型的雜湊碼。我們建立了一個 Long 變數,併為其賦值一個使用正長整型值建立的 Long 物件。然後使用 hashCode() 方法,我們列印 Long 物件的雜湊碼。

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
      Long i = new Long("20");
    
      /* returns a hash code value for this object, equal to the primitive
         long value represented by this Long object */
      int retval = i.hashCode();
      System.out.println("Value = " + retval);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Value = 20

獲取具有負值的 Long 的雜湊碼示例

以下示例演示瞭如何使用 Long hashCode() 方法獲取長整型的雜湊碼。我們建立了一個 Long 變數,併為其賦值一個使用負長整型值建立的 Long 物件。然後使用 hashCode() 方法,我們列印 Long 物件的雜湊碼。

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
      Long i = new Long("-20");
    
      /* returns a hash code value for this object, equal to the primitive
         long value represented by this Long object */
      int retval = i.hashCode();
      System.out.println("Value = " + retval);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Value = 19

獲取具有零值的 Long 的雜湊碼示例

以下示例演示瞭如何使用 Long hashCode() 方法獲取長整型的雜湊碼。我們建立了一個 Long 變數,併為其賦值一個使用零長整型值建立的 Long 物件。然後使用 hashCode() 方法,我們列印 Long 物件的雜湊碼。

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
      Long i = new Long("0");
    
      /* returns a hash code value for this object, equal to the primitive
         long value represented by this Long object */
      int retval = i.hashCode();
      System.out.println("Value = " + retval);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Value = 0

獲取具有負零值的 Long 的雜湊碼示例

以下示例演示瞭如何使用 Long hashCode() 方法獲取長整型的雜湊碼。我們建立了一個 Long 變數,併為其賦值一個使用負零長整型值建立的 Long 物件。然後使用 hashCode() 方法,我們列印 Long 物件的雜湊碼。

package com.tutorialspoint;
public class LongDemo {
   public static void main(String[] args) {
      Long i = new Long("-0");
    
      /* returns a hash code value for this object, equal to the primitive
         long value represented by this Long object */
      int retval = i.hashCode();
      System.out.println("Value = " + retval);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Value = 0
java_lang_long.htm
廣告
© . All rights reserved.