Title: Missing hashCode() Corrupted Payment Batch — Java Object
Open Graph Title: Missing hashCode() Corrupted Payment Batch — Java Object
Description: A HashSet returned size() > 1 for same IDs: hashCode() was missing. The JVM requires both equals() and hashCode() — see how to avoid data corruption.
Open Graph Description: A HashSet returned size() > 1 for same IDs: hashCode() was missing. The JVM requires both equals() and hashCode() — see how to avoid data corruption.
Opengraph URL: https://thecodeforge.io/java/object-class-java/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/java/object-class-java/"
},
"headline": "Missing hashCode() Corrupted Payment Batch — Java Object",
"description": "A HashSet returned size() > 1 for same IDs: hashCode() was missing. The JVM requires both equals() and hashCode() — see how to avoid data corruption.",
"keywords": "Missing hashCode() Corrupted Payment Batch — Java Object, Java, Intermediate, OOP Concepts, A HashSet returned size() > 1 for same IDs: hashCode() was missing. The JVM requires both equals() and hashCode() — see how to avoid data corruption.",
"proficiencyLevel": "Intermediate",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-06T00:00:00+00:00",
"dateModified": "2026-08-30T00:00:00+00:00",
"url": "https://thecodeforge.io/java/object-class-java/",
"inLanguage": "en",
"educationalLevel": "Intermediate",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Java", "item": "https://thecodeforge.io/java/"},
{"@type": "ListItem", "position": 3, "name": "Missing hashCode() Corrupted Payment Batch — Java Object", "item": "https://thecodeforge.io/java/object-class-java/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Does every class in Java extend Object?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes — every class in Java implicitly extends java.lang.Object if it doesn't explicitly extend another class. Even that other class ultimately extends Object somewhere up the chain. Arrays and interfaces are also objects at runtime. It's truly universal."
}
},
{
"@type": "Question",
"name": "What is the default implementation of equals() in Java?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The default equals() inherited from Object checks reference equality — it returns true only if both references point to the exact same object in memory (equivalent to ==). It has no knowledge of your fields. If you want two separate objects with the same data to be considered equal, you must override it."
}
},
{
"@type": "Question",
"name": "Is it safe to only override hashCode() without overriding equals()?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Technically it compiles, but it violates the spirit of the contract and creates confusing behaviour. The equals/hashCode contract runs in one direction: equal objects must have equal hash codes. If you override hashCode() without equals(), two objects that share a hash code still won't be considered equal by collections — they'll both be stored in a HashSet. Always override both or neither."
}
},
{
"@type": "Question",
"name": "How do I choose between instanceof and getClass() in my equals() implementation?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Use instanceof unless you have an explicit design reason to prevent subclass equality. instanceof follows the Liskov substitution principle — a subclass instance can be equal to a parent instance. getClass() enforces stricter type identity — subclass instances are never equal to parent instances. The instanceof approach is more flexible and is the convention used by most Java libraries. If you use getClass(), document it clearly because it breaks the expectation that equals() is symmetric across the class hierarchy."
}
},
{
"@type": "Question",
"name": "Can I use Lombok's @Data or @EqualsAndHashCode in production?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, but understand what they generate. @Data generates equals() and hashCode() from ALL non-static, non-transient fields. This is perfect for value objects but wrong for entities — entities should only use the ID field. For entities, manually specify @EqualsAndHashCode(onlyExplicitlyIncluded = true) and annotate only the ID field with @EqualsAndHashCode.Include. Never blindly trust Lombok defaults — they're designed for the general case, not your specific domain model."
}
},
{
"@type": "Question",
"name": "What happens if I change a field that's used in hashCode() after the object is in a HashSet?",
"acceptedAnswer": {
"@type": "Answer",
"text": "The object becomes permanently unretrievable from that HashSet. When you inserted it, the hash code was computed from the original field values, and the object was placed in the corresponding bucket. After mutation, hashCode() returns a different value. Calling contains() or remove() computes the new hash code, looks in the wrong bucket, and never finds the object. The object leaks until the collection is garbage collected. This is why hashCode() must only use immutable fields."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large