1 / 4

equals 和 hashcode

equals 和 hashcode. Use == to determine  if  two reference variables refer to the same object. Use equals() to determine  if  two objects are meaningfully equivalent. If  x.equals (y) is true, then  x.hashCode () ==  y.hashCode () is  true . If  you override equals(), override  hashCode ().

lavi
Download Presentation

equals 和 hashcode

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. equals 和 hashcode Use == to determine if two reference variables refer to the same object.Use equals() to determine if two objects are meaningfully equivalent.If x.equals(y) is true, then x.hashCode() == y.hashCode() is true.If you override equals(), override hashCode().

  2. equals 和 hashcode public class MainClass { public static void main(String[] argv) { MyClassmyClass = new MyClass(); MainClassmainClass = new MainClass(); System.out.println(myClass.equals(mainClass)); } } class MyClass { public booleanequals(Object object) { if (object instanceofMyClass == false) { return false; } return true; } }

  3. equals 和 hashcode 1. If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.2. If two objects are unequal according to the equals(Object) method, there's no requirement about hashcode().3. If calling hashcode() on two objects produce different integer result, then both of themmust be unequal according to the equals(Object).

  4. Hashcode /equals • Example 0 • Example 1 • Example 2 • Example 3 • Example 4

More Related