Custom Hashing
Learn Custom Hashing (Hashing, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
Writing a Custom hashCode() Any object used as a HashMap/HashSet key needs a hashCode() that is CONSISTENT with equals() : two equal objects must return the same hash code (the reverse isn't required - different objects CAN share a hash, that's just a collision). Use Objects.hash(field1, field2, ...) for a quick, correct implementation. A good hash spreads values evenly across buckets - a hashCode() that always returns the same constant is technically legal but destroys performance (every key collides into one bucket).