Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

467
Views
How to ignore 1 field in Java equals() for custom object?

I have a Person object which has 20 fields and stored in dynamoDB. I want to create a new Person object based on some input, check if the same object exists in the Database or not. If it exists, I want to compare the 2 objects on the basis of 19 fields out of 20. The field to ignore is a boolean flag check.

I am using Lombok @Data to generate the equals method.

Is there a way to do this without having to write a full fledged overriden equals method myself ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can also use Lombok's @EqualsAndHashCode in conjunction with an exclude.

@EqualsAndHashCode
public class MyObject{

    @EqualsAndHashCode.Exclude
    Object fieldToExclude;

}

Instead of choosing which to exclude you can also opt to choose the ones you wish to include like so. All which is not annotated with Include will not be used in the equals & hashcode implementation.

@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class MyObject{

    @EqualsAndHashCode.Include
    Object fieldToInclude;

}

Lombok EqualsAndHashCode documentation

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!