Skip to content
Related Articles

Related Articles

Improve Article

BigInteger hashCode() Method in Java

  • Last Updated : 14 Aug, 2018

The java.math.BigInteger.hashCode() method returns the hash code for this BigInteger. The hashcode is always the same if the object doesn’t change.
Hashcode is a unique code generated by the JVM at the time of object creation. We can use hashcode to perform some operation on hashing related algorithms like hashtable, hashmap etc. We can search an object with that unique code.

Syntax:

public int hashCode()

Returns: The method returns an integer value which represents hashCode value for this BigInteger.

Examples:

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

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

Example 1: Below programs illustrate hashCode() method of BigInteger class






// 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);
    }
}
Output:
hashCode of 32145 : 32145
hashCode of 7613721467324 : -1255493552

Example 2: When both bigInteger has same value




// 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);
    }
}
Output:
hashCode of 4326516236135 : 1484200280
hashCode of 4326516236135 : 1484200280

Reference:
BigInteger hashCode() Docs

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :