Java Program to Illustrate the Use of HashCode() Method

This is a Java Program to Illustrate the Use of HashCode() Method. The hashcode of a Java Object is simply a number, it is 32-bit signed int, that allows an object to be managed by a hash-based data structure.

We have to implement hashcode() method of a class in such way that if two objects are equals, ie compared by equal() method of that class, then those two objects must return same hash code. The below example shows how to override equals and hashcode methods. The class Price overrides equals and hashcode. If you notice the hashcode implementation, it always generates unique hashcode for each object based on their state, ie if the object state is same, then you will get same hashcode. A HashMap is used in the example to store Price objects as keys. It shows though we generate different objects, but if state is same, still we can use this as key.

Here is the source code of the Java Program to Illustrate the Use of HashCode() Method. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.HashMap;
  2. public class HashDemo
  3. {
  4.    public static void main(String a[])
  5.    {
  6.         HashMap<Price, String> hm = new HashMap<Price, String>();
  7.         hm.put(new Price("Banana", 20), "Banana");
  8.         hm.put(new Price("Apple", 40), "Apple");
  9.         hm.put(new Price("Orange", 30), "Orange");   //creating new object to use as key to get value
  10.         Price key = new Price("Banana", 20);
  11.         System.out.println("Hashcode of the key: "+key.hashCode());
  12.         System.out.println("Value from map: "+hm.get(key));
  13.     }
  14. }
  15. class Price
  16. {
  17.     private String item;
  18.     private int price;
  19.     public Price(String itm, int pr)
  20.     {
  21.         this.item = itm;
  22.         this.price = pr;
  23.     }
  24.     public int hashCode()
  25.     {
  26.         System.out.println("In hashcode");
  27.         int hashcode = 0;
  28.         hashcode = price*20;
  29.         hashcode += item.hashCode();
  30.         return hashcode;
  31.     }
  32.     public boolean equals(Object obj)
  33.     {
  34.         System.out.println("In equals");
  35.         if (obj instanceof Price)
  36.         {
  37.             Price pp = (Price) obj;
  38.             return (pp.item.equals(this.item) && pp.price == this.price);
  39.         }
  40.         else
  41.         {
  42.             return false;
  43.         }
  44.     }
  45.     public String getItem()
  46.     {
  47.         return item;
  48.     }
  49.     public void setItem(String item)
  50.     {
  51.         this.item = item;
  52.     }
  53.     public int getPrice()
  54.     {
  55.         return price;
  56.     }
  57.     public void setPrice(int price)
  58.     {
  59.         this.price = price;
  60.     }
  61.     public String toString()
  62.     {
  63.         return "item: "+item+"  price: "+price;
  64.     }
  65. }

Output:

$ javac HashDemo.java
$ java HashDemo
 
In hashcode
In hashcode
In hashcode
In hashcode
Hashcode of the key: 1982479637
In hashcode
In equals
Value from map: Banana

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.