java-overwrite equals must overwrite Hashchode () __java

Source: Internet
Author: User
Class H {
private int x;
private int y;
private float Z;
Private Double T;

H (int x, int y,float z, double t) {
this.x = x;
This.y = y;
This.z = Z;
THIS.T = t;
}

public boolean equals (Object o) {
if (o = = null) return false;
if (o = = this) return true;
if (!) ( o instanceof H)) return false;
H p = (h) O;
int dz = Float.floattointbits (z);
Long ft = double.doubletolongbits (t);
Return ((this.x = = p.x) && (this.y = = p.y) && (dz = float.floattointbits (p.z)) && (ft = = DOUBLE.D Oubletolongbits (P.T)));
Return ((this.x = = p.x) && (this.y = = p.y) && (this.z = p.z) && (this.t = = p.t));
}

public int hashcode () {
int result = 17;
result = 37*result + x;
result = 37*result + y;
result = 37*result + float.floattointbits (z);
result = 37*result + (int) double.doubletolongbits (t);
return (int) result;
}

public static void Main (string[] args) {

H p1 = new H (1,1,-0.0f,0.000000000000000000001);
h P2 = new H (1,1,-0.0f,0.000000000000000000001);
System.out.println (P1.equals (p2));
System.out.println (P1.hashcode ());
System.out.println (P2.hashcode ());
}
}
===================================================================
True
-1946841466
-1946841466

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.