Card 08/ 08

RecapDifficulty: Advanced1 min

Recap: equals, hashCode and toString

The defaults, the contract, how a map looks up a key, and the two ways keys get lost, with where each was.

The three inherited methods come down to the five ideas below, each listed with the card that covers it.

What the topic established, and which card it was on
The ideaWhere
Every class inherits equals, hashCode and toString with defaultsequals, hashCode and toString: The Methods Every Object Inherits
The default equals compares addresses, not contentsequals Example: Two Identical Orders That Are Not Equal
Equal objects must have equal hash codes; equal hash codes need not mean equal objectsThe equals and hashCode Contract
hashCode chooses the bucket; equals decides what was found in itHow a HashMap Uses hashCode and equals: A Walkthrough
Override the two together, from fields that never changeMutable HashMap Keys: Changing a Key Loses the Entry

Breaking the contract throws nothing. It makes a value you stored impossible to find again and leaves size() insisting it is there, which is why this is worth getting right before anything depends on it.