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); } } |
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); } } |
hashCode of 4326516236135 : 1484200280 hashCode of 4326516236135 : 1484200280
Reference:
BigInteger hashCode() Docs
Recommended Posts:
- HashSet hashCode() method in Java with Example
- Integer hashCode() Method in Java
- Vector hashCode() Method in Java
- Set hashCode() method in Java with Examples
- ConcurrentLinkedDeque hashCode() method in Java with Example
- Stack hashCode() method in Java with Example
- YearMonth hashCode() method in Java
- GregorianCalendar hashCode() Method in Java
- LinkedHashSet hashCode() method in Java with Example
- LinkedBlockingDeque hashCode() method in Java with Example
- TreeSet hashCode() method in Java with Example
- BigDecimal hashCode() Method in Java
- IdentityHashMap hashCode() Method in Java
- Map hashCode() Method in Java with Examples
- CopyOnWriteArrayList hashCode() method in Java
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.



