[Valid Java] 5. overwrite the hashcode and mongoshashcode when overwriting equals.

Source: Internet
Author: User

[Valid Java] 5. overwrite the hashcode and mongoshashcode when overwriting equals.

Package cn. xf. cp. ch02.item9; import java. util. hashMap; import java. util. map; public class PhoneNumber {private final short areaCode; private final short prefix; private final short lineNumber; public PhoneNumber (int areaCode, int prefix, int lineNumber) {rangeCheck (areaCode, 999, "area code"); rangeCheck (prefix, 999, "prefix"); rangeCheck (lineNumber, 9999, "line number"); this. areaCode = (short) area Code; this. prefix = (short) prefix; this. lineNumber = (short) lineNumber;} private static void rangeCheck (int arg, int max, String name) {if (arg <0 | arg> max) throw new IllegalArgumentException (name + ":" + arg) ;}@ Override public boolean equals (Object o) {if (o = this) return true; if (! (O instanceof PhoneNumber) return false; PhoneNumber pn = (PhoneNumber) o; return pn. lineNumber = lineNumber & pn. prefix = prefix & pn. areaCode = areaCode;}/* @ Override // as to why 31 is used, this is the recommended value. Studies have shown that this number provides better performance in public int hashCode () {int result = 17; result = 31 * result + areaCode; result = 31 * result + prefix; result = 31 * result + lineNumber; return result ;} * /// if an object does not change frequently and the overhead is large, you need to take the test. The hash code is cached inside the object // The variable modified with volatile. Every time a thread uses a variable, it reads the modified value of the variable. Private volatile int hashcode; @ Override public int hashCode () {int result = hashcode; if (result = 0) {result = 17; result = 31 * result + areaCode; result = 31 * result + prefix; result = 31 * result + lineNumber; hashcode = result;} return result;} public static void main (String [] args) {Map <PhoneNumber, string> m = new HashMap <PhoneNumber, String> (); m. put (new PhoneNumber (707,867,530 9), "Jenny"); // jenny is not returned here, and null is returned, this is because put objects are put in different hash buckets. out. println (m. get (new PhoneNumber (707,867,530 9 )));}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.