1.

Explain hashCode() and equals() in Java.

Answer»

 HashMap is part of the Java collection framework. HashMap uses a technique known as hashing. Hashing is the process of CONVERTING an object into an integer value. Indexing and search speed are AIDED by the integer value. It is in charge of CREATING the map interface. It stores the data in a Key/Value PAIR. In HashMap, the node is represented as a class that holds an array of nodes. Internally, it uses an array and LinkedList data structure to hold Key and Value. There are four fields in HashMap.

  • equals() is a function that compares two objects to see if they are equal. It compares the Key to see if they are equivalent. It belongs to the Object class and is a method. It is possible to override it. It is required to override the hashCode() method if you override the equals() method.
  • hashCode() is an object class method. It returns the object's memory reference in integer form. The bucket number is determined by the value returned by the method. The address of the element WITHIN the map is represented by the bucket number. Null Key's hash code is 0.


Discussion

No Comment Found