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.