Hashing

  • It is a method of storing and retrieving data from database in O(1) time.
  • Key terms are
    • Search key
      • Key basis On which we store and search for data.
    • Hash table
      • Data structure for storing hash data.
    • Hash function
      • K mod 10,K mod n, Midsquare, Folding Method.
Internal working of a Hashset using HashMap
  • When we add values to hash set, we add(Key,PRESENT) in internal HashMap.
  • Public Boolean add (E e){return map.put(e,PRESENT)==null;}
Overriding hashcode and equals method
  • When we want object of any class to work as a key for us, we override hash code and equals method in our class.
  • If we want to compare two objects based on their values using equals, we will override equal method and hash code method.
  • By default, equal method compares the reference of objects only.
  • When we want to compare two objects And we wanted to treat them as equal On the basis of equality of the property. Then we should override equals and hash code method.
  • For example, if we want to employee ID of employee class to be used for our hash code

No comments:

Post a Comment