equals, hashCode and toString

Java equals, hashCode and toString: the defaults every object inherits, the contract between equals and hashCode, and how hash maps lose keys.

  • 8 cards
  • 6 questions
  • About 10 min
  • Intermediate to Advanced
Share

Three methods every Java object inherits: equals, which decides whether two objects count as the same, hashCode, which hash-based collections use to find an object quickly, and toString, which turns an object into text. Two of them have a contract between them that nothing enforces, and breaking it does not throw: it makes a value you stored impossible to find again. It covers the defaults, the contract, how a HashMap uses both methods, and two ways keys get lost. It leaves out compareTo and sorted collections.

Contents

8 cardsnot started