BigInteger hashCode() Method in Java

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

hashCode of 32145 : 32145
hashCode of 7613721467324 : -1255493552

Example 2: When both bigInteger has same value

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

hashCode of 4326516236135 : 1484200280
hashCode of 4326516236135 : 1484200280

Reference:
BigInteger hashCode() Docs



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.