The basic concept

  • Object is called to compare whether two objects are equalequals()Methods:
    • Determines whether object addresses referred to by object references are equal
  • When the object addresses are equal,Then the object related data is also equal, including:
    • Handle to the object
    • Object head
    • Object instance data
    • Object type data
  • You can determine whether objects are equal by comparing their addresses

The Object source

  • Object is used without overwritingObjectIn theequals()Methods andhashCode()methods
    • Equals (): Checks whether references to two objects refer to the same object
    • HashCode (): Generates an integer value based on the object address
  • The Object hashCode() method is native: indicates that the method is implemented by the operating system. Java invokes the underlying operating system code to obtain the Hash value
public native int hashCode(a);
Copy the code

Rewrite equals

  • Scenario for overriding equals() :
    • Let’s say I have a bunch of students
    • By default, to determine whether multiple student objects are equal, you need to determine by address:
      • If the addresses of objects are equal, the data of the object instances must be the same
    • To judge the requirements of equality:
      • Objects are considered equal when students are equal in name, age, sex,
      • It is not necessary that the addresses of the objects be identical
  • After overriding equals(), it prints [s1==s2]
  • If you hadn’t overridden equals(), you would have printed [s1!=s2].

Override hashCode

  • According to the override equals method, s1 and s2 above are considered equal

  • The hashCode() method in Object:

    • Multiple calls to the hashCode() method of the same object must return the same positive number, as long as equals() has not been modified
    • If two objects are equals() to each other, the hashcode values of the two objects must be equal
    • Generating different Hashcodes for different objects can improve the performance of Hash tables
  • Implementation of HashSet

    • The underlying HashSet is implemented through a HashMap
    • Comparing elements in a Set for equality is done by comparing the object’s Hashcode
  • HashCode ()

    • Start by sorting out the attributes that determine the equality of objects
    • Then take the smallest possible positive integer to prevent the end result from exceeding the range of integers of int
    • Then calculate [positive integer * hashCode for attribute + hashCode for one of the remaining attributes]
    • Repeat steps

The principle of analysis

  • Because the parent Object’s hashCode() method is not overridden, Object’s hashCode() method generates the hashCode of the response based on the addresses of the two objects
  • Since the two objects are separate instances created by the entity class, the addresses must be different, and so the hashCode values will be different
  • Set is not the only standard for distinguishing objects:
    • Whether the hashCode values of two objects are the same
    • Then check whether the two objects are equal
  • Map is not the only standard for distinguishing objects:
    • Get the save array subscript based on the HashCode assignment of the Key value
    • Eaquals is then used to distinguish whether it is a unique value

HashMap

HashMap constructs

  • HashMap: Consists of arrays and linked lists

A HashMap of storage

  • Storage of HashMap:
    • The location of an object stored in a HashMap is determined by the hashcode value of the key
    • A HashMap lookup key:
      • When searching for a key, the hashMap will first locate the array based on the hashCode of the key value through the mod algorithm
      • We then get the corresponding object by matching the same key value according to the key’s equals method
  • Storage rules:
    • The Key’s hashcode and the capacity of HashMap are combined to obtain the subscript of the Key stored in the array
  • A HashMap lookup key:
    • Gets the key’s position in the array
    • The corresponding key value object is matched
    • The above objects are then matched against key.equals() to the data objects that get the corresponding keys
  • In the HashMap hashCode:
    • The absence of HashCode means that the HashMap is stored in a random way
    • Each time you use map.get(), you need to match each object in the map to equals, resulting in inefficiency