equals() & hashCode()
Në shqip përdoret i njëjti emër.
ShpjegimiSQ
equals() vendos kur dy objekte janë „të njëjtë“ sipas vlerës, dhe hashCode() jep një numër për to që përdorin HashMap dhe HashSet. Rregulli i artë: po e mbishkrove equals(), mbishkruaj edhe hashCode() — përndryshe HashMap-i s'i gjen objektet.
EnglishEN
equals() decides when two objects are “the same” by value, and hashCode() gives them a number used by HashMap and HashSet. The golden rule: if you override equals(), override hashCode() too — otherwise HashMap can't find your objects.
Si ta mendosh
Si dy bileta për të njëjtën ndeshje dhe të njëjtin vend: janë dy copa letre, por vlejnë njësoj.
Lexoje në anglisht
Like two tickets for the same match and the same seat: two pieces of paper, but worth the same.
Shembull kodijava
@Override public boolean equals(Object o) {
return o instanceof Pika p && p.x == x && p.y == y;
}
@Override public int hashCode() { return Objects.hash(x, y); }