Java BigInteger hashCode()方法
java.math.BigInteger.hashCode() 方法返回此BigInteger的哈希代码。如果对象没有变化,哈希码总是相同的。
哈希码是JVM在创建对象时生成的唯一代码。我们可以使用哈希码来执行一些与哈希算法有关的操作,如hashable、hashmap等。我们可以用这个唯一的代码来搜索一个对象。
语法
public int hashCode()
返回: 该方法返回一个整数值,代表此BigInteger的hashCode值。
例子
输入: BigInteger1=32145
输出: 32145
解释: BigInteger1.hashCode()=32145.
输入: BigInteger1=7613721467324
输出: -1255493552
解释: 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