hashCode, HashMap principle

hashCode contract is: if two objects are equal, then calling two objects hashCode () method will return the same hash value.

HashMap in buckets principle:

  1. When you store a value in hashMap, these values ​​are actually stored in a set of buckets. Each bucket is assigned a number to identify it. When you're in the HashMap put a value, and it will store the data in those buckets. Specifically stored in barrels which, depending on your object hashcode returned. In other words, if an object calls hashCode () method returns the 49, it will be stored in HashMap No. 49 of the bucket.
  2. Then, when you try to contain the element by calling contains (element) method to check the collection. HashMap hashCode will first get this element, and then, it looks at the bucket and the corresponding hashCode. If the bucket is empty, it means that we have completed, and returns false, which means HashMap does not contain the element.
  3. If you have one or more objects in the bucket, it will use equals that you define () function element is compared to all the other elements of the buckets.

Source: https://mp.weixin.qq.com/s/VqKjRX5bRPQC9-TiO5ByOA

Guess you like

Origin www.cnblogs.com/cag2050/p/11319727.html
Recommended