Java / Lombok Interview questions
What is @EqualsAndHashCode used for?
@EqualsAndHashCode generates proper equals() and hashCode()
overrides based on the class's fields, following the standard contract Java expects — two objects with
equal field values are considered equal and produce the same hash code, which is essential for correct
behavior in collections like HashSet or HashMap.
@EqualsAndHashCode public class Point { private int x; private int y; } // two Point(1, 2) instances are now equal, and share the same hashCode
By default it includes all non-static fields, but specific fields can be excluded with
@EqualsAndHashCode.Exclude — useful for fields that shouldn't factor into equality, such as
a timestamp or a cached, derived value that doesn't represent the object's actual identity.
More Related questions...