Java中的BigInteger hashCode()方法

这个 JAVA数学大整数。hashCode() 方法返回此BigInteger的哈希代码。如果对象不变,哈希代码总是相同的。 Hashcode是JVM在创建对象时生成的唯一代码。我们可以使用哈希代码对哈希相关算法执行一些操作,比如 哈希表 , 散列表 等等。我们可以用那个唯一的代码搜索一个对象。

null

语法:

public int hashCode()

返回: 该方法返回一个整型值,该值表示此BigInteger的hashCode值。

例如:

Input: BigInteger1=32145
Output: 32145
Explanation: BigInteger1.hashCode()=32145.

Input: BigInteger1=7613721467324 
Output: -1255493552
Explanation: BigInteger1.hashCode()=-1255493552.

示例1:下面的程序演示了BigInteger类的hashCode()方法

// Java program to demonstrate
// hashCode() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigInteger objects
BigInteger b1, b2;
b1 = new BigInteger( "32145" );
b2 = new BigInteger( "7613721467324" );
// apply hashCode() method
int hashCodeOfb1 = b1.hashCode();
int hashCodeOfb2 = b2.hashCode();
// print hashCode
System.out.println( "hashCode of "
+ b1 + " : " + hashCodeOfb1);
System.out.println( "hashCode of "
+ b2 + " : " + hashCodeOfb2);
}
}


输出:

hashCode of 32145 : 32145
hashCode of 7613721467324 : -1255493552

示例2:当两个bigInteger的值相同时

// Java program to demonstrate
// hashCode() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigInteger objects
BigInteger b1, b2;
b1 = new BigInteger( "4326516236135" );
b2 = new BigInteger( "4326516236135" );
// apply hashCode() method
int hashCodeOfb1 = b1.hashCode();
int hashCodeOfb2 = b2.hashCode();
// print hashCode
System.out.println( "hashCode of "
+ b1 + " : " + hashCodeOfb1);
System.out.println( "hashCode of "
+ b2 + " : " + hashCodeOfb2);
}
}


输出:

hashCode of 4326516236135 : 1484200280
hashCode of 4326516236135 : 1484200280

参考: BigInteger hashCode()文档

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享