Library › Coding AI › Software developers

Update equals and hashCode together for a new rule

Practice: Giving the right context. About 5 minutes.

About this exercise

The mailing-list job sends duplicates because Member overrides equals but not hashCode. Product also just decided that team no longer matters: the same email (ignoring case) is the same person, whatever the team. Update equals to compare only email with equalsIgnoreCase, and add a matching hashCode using email.toLowerCase(Locale.ROOT). Return only the two methods, each with @Override, in exactly 1 code block of at most 12 lines, with under 20 words of text.

Common mistakes when you ask AI for this

  • Not passing on the product decision. The AI only sees the current equals, which still compares team. Try: Add: "Team no longer matters: equals compares only email, ignoring case."
  • A hashCode that disagrees. Hashing team, or the raw email, breaks HashSet when equals ignores case. Try: Add: "hashCode uses only email.toLowerCase(Locale.ROOT)."
  • A rewritten class. The AI reprints the class, sometimes as a record, which the JSON mapper can't use. Try: Add: "Return only equals and hashCode, each with @Override."
  • An essay on the contract. The AI explains equals and hashCode at length. Try: Add: "One code block, no text."

What a strong prompt includes

  • Passes on the new rule
  • Handles case
  • Names the helper
  • Asks for only the two methods

Practice this now ↑

Related exercises