This vulnerability occurs when a Java class defines either the equals() method or the hashCode() method, but not both, breaking a fundamental contract of object equality.
In Java, the `equals()` and `hashCode()` methods work as a pair to define object identity for collections like `HashMap` and `HashSet`. When you override only one of these methods, you violate a core rule: if two objects are considered equal by the `equals()` method, they must return the same `hashCode()` value. Failing to uphold this contract causes unpredictable behavior in hash-based collections, leading to objects that are 'equal' being stored separately, becoming impossible to retrieve, or causing duplicate entries. To fix this, always override both methods together, ensuring their logic is based on the same set of object attributes. Use your IDE's generator or a library like Lombok to maintain consistency. This ensures your objects behave correctly in all standard Java collections and prevents subtle, hard-to-debug errors in your application's data handling.
Impact: Other
If this invariant is not upheld, it is likely to cause trouble if objects of this class are stored in a collection. If the objects of the class in question are used as a key in a Hashtable or if they are inserted into a Map or Set, it is critical that equal objects have equal hashcodes.